Skip to content

runtime.Caller() return wrong number of line #663

Closed
@gopherbot

Description

@gopherbot

by izmalych:

What steps will reproduce the problem?

main.go:
package main

import (
        "fmt";
        "runtime";
        )

func main() {
        log("line # 9")
        fmt.Println("line # 10")
        log("line # 11")
        fmt.Println("line # 12")
}


func log(message string){
        _, file, line, ok := runtime.Caller(1)
        if !ok { fmt.Println("some error"); return }
        fmt.Printf("%s:%d %s\n", file, line, message)
}

What is the expected output?
./main.out
/home/alexni/workspace/go/test/main.go:9 line # 9
line # 10
/home/alexni/workspace/go/test/main.go:11 line # 11
line # 12

What do you see instead?
./main.out
/home/alexni/workspace/go/test/main.go:10 line # 9
line # 10                            ^^^^      
/home/alexni/workspace/go/test/main.go:12 line # 11
line # 12                            ^^^^


What is your $GOOS?  $GOARCH?
GOARCH=amd64
GOOS=linux




Which revision are you using?  (hg identify)

2dc7f4773836 tip

6g version 5040

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions