Java地带 - GoLang
type: channel
link: https://blog.apires.cn/category/GoLang/
items: Golang的Log打印行号等信息

Golang的Log打印行号等信息
type: item
date: 2024-03-06T09:10:00+08:00
description: 默认情况下，log模块的只打印日期和时间, 没具体行号。package main /* #include <stdlib.h> */ import "C" import "log" func main() { log.SetFlags(log.Lshortfile | log.LstdFlags) log.Println( C.random()) }打印结果:2022/05/25 22:35:55 X.cgo1.go:14: 1804289383其中, log.Lshortfile 还可以设置为log.Llongfile 即完整文件路径获取当前行数,文件名,函数名(方法名):package main import ( "runtime" "fmt" ) func main() { funcName, file, line, ok := runtime.Caller(0) if ok { fmt.Println("Func Name=" + runtime.FuncForPC(funcName).Name()) fmt.Printf("file: %s line=%d\n", file, line) } }参考：http://wendal.net/446.html