Line directives in Go

This is an example Go program showing the use of line-directives.

//line bogus.go:100
package main

import (
        "fmt"
        "runtime"
)

func main() {
        _, file, line, _ := runtime.Caller(0)
        fmt.Printf("%s:%d: line is now here.\n", file, line);
        //line monster.go:33
        _, file, line, _ = runtime.Caller(0)
        fmt.Printf("%s:%d: line is now here.\n", file, line);
}

(download)

The output of the example looks like this:

bogus.go:108: line is now here.
monster.go:33: line is now here.

As with C line directives, the line number in the directive refers to the next line of the file.


Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer