LoginSignup
28
30

More than 5 years have passed since last update.

Go言語でプリント文デバッグするときのTips

Last updated at Posted at 2016-02-17

Go言語でどのファイルのどの行を通っているのか確認する簡単な方法です。
次のようにlogのインスタンスを作成して、空のPrintln()を呼ぶだけで、ファイル名と行番号が表示できます。

g.go
package main

import "log"
import "os"

var tracer = log.New(os.Stdout, "", log.Lshortfile)

func main() {
  tracer.Println()
  tracer.Println()
  os.Exit(1)
  tracer.Println()
  tracer.Println()
}

実行するとこんな感じ。

$ go run g.go
g.go:9: 
g.go:10: 
exit status 1

結果をファイルに落として、vimのQuickFixに渡すことも可能です。

$ vim -q result.txt

C言語みたいに __FILE____LINE__ を使わなくてよいので楽ですね。

28
30
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
30