Golang进阶笔记

Golang进阶笔记#

golang

性能分析#

import (
    "net/http"
    _ "net/http/pprof"
)

go func() {
    log.Println(http.ListenAndServe("localhost:6060", nil))
}()

将以上代码插入程序中 启动之后执行

go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30

具体使用方法可以help 如果使用top的话就可以查看方法占用了

(pprof) top
Showing nodes accounting for 29.59s, 98.60% of 30.01s total Dropped 35 nodes (cum <= 0.15s)
flat flat% sum% cum cum%
28.98s 96.57% 96.57% 28.98s 96.57% time.Now
0.24s 0.8% 97.37% 0.51s 1.70% runtime.selectgo
0.16s 0.53% 97.90% 0.16s 0.53% memeqbody
0.13s 0.43% 98.33% 0.29s 0.97% runtime.mapaccess2_faststr 
0.08s 0.27% 98.60% 29.95s 99.80% example.com//pts/services/monitor.Run

如以上就是说monitor.Run方法中使用了select, 然后其中的time.Now被疯狂执行.

路径问题#

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.Contains(dir, getTmpDir()) {
		return getCurrentAbPathByCaller()
	}
	return dir
}

func getCurrentPath() string {
	_, filename, _, _ := runtime.Caller(1)

	return path.Dir(filename)
}

// 获取系统临时目录,兼容go run
func getTmpDir() string {
	dir := os.Getenv("TEMP")
	if dir == "" {
		dir = os.Getenv("TMP")
	}
	res, _ := filepath.EvalSymlinks(dir)
	return res
}

// 获取当前执行文件绝对路径
func getCurrentAbPathByExecutable() string {
	exePath, err := os.Executable()
	if err != nil {
		log.Fatal(err)
	}
	res, _ := filepath.EvalSymlinks(filepath.Dir(exePath))
	return res
}

// 获取当前执行文件绝对路径(go run)
func getCurrentAbPathByCaller() string {
	var abPath string
	_, filename, _, ok := runtime.Caller(0)
	if ok {
		abPath = path.Dir(filename)
	}
	return abPath
}

输出#

ian@ianDebian:~$ ./test
getTmpDir(当前系统临时目录) =  .
getCurrentAbPathByExecutable(仅支持go build) =  /home/ian
getCurrentAbPathByCaller(仅支持go run) =  /home/ian
getCurrentAbPath(最终方案-全兼容) =  /home/ian
getCurrentPath(runtime.Caller1) =  /home/ian
ian@ianDebian:~$
ian@ianDebian:~$ go run test.go
getTmpDir(当前系统临时目录) =  .
getCurrentAbPathByExecutable(仅支持go build) =  /tmp/go-build3048077768/b001/exe
getCurrentAbPathByCaller(仅支持go run) =  /home/ian
getCurrentAbPath(最终方案-全兼容) =  /tmp/go-build3048077768/b001/exe
getCurrentPath(runtime.Caller1) =  /home/ian
ian@ianDebian:~$
ian@ianDebian:~$ go test test_test.go test.go -v
=== RUN   TestHelloWorld
    test_test.go:8: getCurrentPath:  /home/ian
--- PASS: TestHelloWorld (0.00s)
PASS
ok  	command-line-arguments	0.002s

Golang 私有仓库#

xxx替换为具体地址