go常用命令

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)
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

anssummer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值