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
+9.9k Golang : Sort and reverse sort a slice of integers
+11.7k How to tell if a binary(executable) file or web application is built with Golang?
+6.8k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+12.3k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+14.5k Golang : Rename directory
+4.7k Linux/MacOSX : How to symlink a file?
+5k Golang : micron to centimeter example
+30k Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
+6.9k Golang : How to solve "too many .rsrc sections" error?
+10.6k Golang : Bubble sort example
+11k Golang : Generate random elements without repetition or duplicate
+13.3k Golang : Linear algebra and matrix calculation example