1.go官方网站: godoc -http ':8989'
2.一般并发的bug 有两种,死锁(block)和 竞争(race)
死锁发生时,go run 会直接报错
race 发生时,要加race 才会在运行时报warning,go run xxx.go 后面加上 -race 参数
3.profile 分析
pprof有两个包用来分析程序一个是net/http/pprof另一个是runtime/pprof,net/http/pprof只是对runtime/pprof包进行封装并用http暴露出来
#pprof分析web项目,非常的简单只需要导入包即可。
import (
_ "net/http/pprof"
)
func main(){
go func(){
http.ListenAndServe(":9909", nil )
}
//main logic
}
4.go install 与go build
go build当前目录下,go install工作目录下的bin目录下
5.go vet与go tool vet
go vet
是一个用于检查Go语言源码中静态错误的简单工具。
6. pprof
Go 中监控代码性能的有两个包:
- net/http/pprof
- runtime/pprof
这两个包都是可以监控代码性能的, 只不过net/http/pprof是通过http端口方式暴露出来的,内部封装的仍然是runtime/pprof。
go tool pprof mytest mytest.prof
7.lint
Golint用于检查go代码中不够规范的地方。
8.printf
v 值的默认格式。
%+v 添加字段名(如结构体)
%#v 相应值的Go语法表示
%T 相应值的类型的Go语法表示
%% 字面上的百分号,并非值的占位符
对于%v来说
bool: %t
int, int8 etc.: %d
uint, uint8 etc.: %d, %x if printed with %#v
float32, complex64, etc: %g
string: %s
chan: %p
pointer: %p
9. var _的作用
用在变量
type Car interface {
run()
}
type Honda struct {
}
func (s Honda)run() {
}
var _ Car = Honda{}
上面用来判断 type Honda是否实现了接口 Car, 用作类型断言,如果Honda没有实现借口Car,则编译错误.
编译期间执行,所以可以用来初始化...
package main
import "fmt"
func main() {
}
// make sure that all the initialization happens before the init() functions
// are called, cf https://golang.org/ref/spec#Package_initialization
var _ = initDebug()
//这是在编译期间就执行
func initDebug() bool {
fmt.Println("in the initDebug happens before the init()")
return true
}
//运行期间执行的
func init(){
fmt.Println("in the init after initDebug")
}
10. go link
package time
// Sleep pauses the current goroutine for at least the duration d.
// A negative or zero duration causes Sleep to return immediately.
func Sleep(d Duration)
//runtime.time
// timeSleep puts the current goroutine to sleep for at least ns nanoseconds.
//go:linkname timeSleep time.Sleep
func timeSleep(ns int64) {
if ns <= 0 {
return
}
gp := getg()
t := gp.timer
if t == nil {
t = new(timer)
gp.timer = t
}
t.f = goroutineReady
t.arg = gp
t.nextwhen = nanotime() + ns
gopark(resetForSleep, unsafe.Pointer(t), waitReasonSleep, traceEvGoSleep, 1)
}