Golang go/token.File.AddLineInfo function example
package go/token
AddLineInfo adds alternative file and line(3rd parameter) number information for a given file offset (1st parameter). The offset must be larger than the offset for the previously added alternative line info and smaller than the file size; otherwise the information is ignored.
AddLineInfo is typically used to register alternative position information for //line filename:line comments in source files.
Golang go/token.File.AddLineInfo function usage example
func (s *Scanner) interpretLineComment(text []byte) {
if bytes.HasPrefix(text, prefix) {
// get filename and line number, if any
if i := bytes.LastIndex(text, []byte{':'}); i > 0 {
if line, err := strconv.Atoi(string(text[i+1:])); err == nil && line > 0 {
// valid //line filename:line comment
filename := string(bytes.TrimSpace(text[len(prefix):i]))
if filename != "" {
filename = filepath.Clean(filename)
if !filepath.IsAbs(filename) {
// make filename relative to current directory
filename = filepath.Join(s.dir, filename)
}
}
// update scanner position
s.file.AddLineInfo(s.lineOffset+len(text)+1, filename, line) // +len(text)+1 since comment applies to next line
}
}
}
}
References :
Advertisement
Something interesting
Tutorials
+11.2k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+17.7k How to enable MariaDB/MySQL logs ?
+5.2k Golang : Issue HTTP commands to server and port example
+6.3k Golang : Selection sort example
+4.7k Fix Google Analytics Redundant Hostnames problem
+28.7k Golang : Detect (OS) Operating System
+14.5k Golang : How to determine if user agent is a mobile device example
+9.9k Golang : Sort and reverse sort a slice of integers
+37.5k Upload multiple files with Go
+16.9k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+20k Golang : How to run your code only once with sync.Once object