expansion

Golang进阶笔记

May 19, 2021
note, golang, expansion
golang, expansion

Golang进阶笔记 # 路径问题 # test_test.go # package main import "testing" func TestHelloWorld(t *testing.T) { // t.Fatal("not implemented") path := getCurrentPath() t.Log("getCurrentPath: ", path) } test.go # package main import ( "fmt" "log" "os" "path" "path/filepath" "runtime" "strings" ) func main() { fmt.Println("getTmpDir(当前系统临时目录) = ", getTmpDir()) fmt.Println("getCurrentAbPathByExecutable(仅支持go build) = ", getCurrentAbPathByExecutable()) fmt.Println("getCurrentAbPathByCaller(仅支持go run) = ", getCurrentAbPathByCaller()) fmt.Println("getCurrentAbPath(最终方案-全兼容) = ", getCurrentAbPath()) fmt.Println("getCurrentPath(runtime.Caller1) = ", getCurrentPath()) } // 最终方案-全兼容 func getCurrentAbPath() string { dir := getCurrentAbPathByExecutable() if strings. ...

Golang进阶笔记

May 19, 2021
note, golang, expansion
golang, expansion

Golang进阶笔记 # 路径问题 # test_test.go # package main import "testing" func TestHelloWorld(t *testing.T) { // t.Fatal("not implemented") path := getCurrentPath() t.Log("getCurrentPath: ", path) } test.go # package main import ( "fmt" "log" "os" "path" "path/filepath" "runtime" "strings" ) func main() { fmt.Println("getTmpDir(当前系统临时目录) = ", getTmpDir()) fmt.Println("getCurrentAbPathByExecutable(仅支持go build) = ", getCurrentAbPathByExecutable()) fmt.Println("getCurrentAbPathByCaller(仅支持go run) = ", getCurrentAbPathByCaller()) fmt.Println("getCurrentAbPath(最终方案-全兼容) = ", getCurrentAbPath()) fmt.Println("getCurrentPath(runtime.Caller1) = ", getCurrentPath()) } // 最终方案-全兼容 func getCurrentAbPath() string { dir := getCurrentAbPathByExecutable() if strings. ...