Skip to content

Instantly share code, notes, and snippets.

@sh7ning
Last active June 2, 2021 07:56
Show Gist options
  • Save sh7ning/066a5232c8e717b613c8857475932328 to your computer and use it in GitHub Desktop.
Save sh7ning/066a5232c8e717b613c8857475932328 to your computer and use it in GitHub Desktop.
Golang笔记

Golang Note

Ide 配置

  • Tools -> file wathers 中增加:
    • go fmt
      • 手动处理: gofmt -l -w .
    • goimports
      • 手动处理: goimports -l -w .
    • 自定义: go二进制 + 参数

go mod

  • 设置环境变量

    export GO111MODULE=on 
    
  • 在项目中执行 go mod init [module] 生成 go.mod文件

  • 执行go mod vendor生成vendor文件

  • 依赖按照配置

go env -w GOPROXY=https://goproxy.cn,direct
go env -w GOPRIVATE=*.gitlab.demo.net
  • 安装依赖go get -u -m xxx@v1.0.0,如果拉不到项目增加参数 go get -u -insecure -v xxx@v1.0.0

  • 获取所有的依赖 go list -m all

  • replace 用法:

    1. go.mod中增加
     replace github.com/Kucoin/kucoin-go-sdk => github.com/sh7ning-mirror/kucoin-go-sdk v1.0.8
    
    1. 执行 go get -u -m github.com/Kucoin/kucoin-go-sdk

语法检测

  • 分析潜在的bug: go vet .

语法相关

Map

  • 从map中删除元素:

    delete(timeZone, "PDT")
    

获取一个变量类型

  • 通过反射获取,如:

    reflect.TypeOf(i)
    
  • i.(type) 只能在switch中使用,函数没有返回值,如:

    func checkType(i interface{}) string {
    	switch i.(type) {
    	case string:
    		return "string"
    	case int:
    		return "int"
    	default:
    		return "unknown"
    	}
    }
    
  • 采用断言的方式,如:

    func getString(i interface{}) string {
    	v, ok := i.(string)
    	if ok {
    		return "type: string, v: " + v
    	}
    
    	return "unknown"
    }
    

如果只是需要知道类型,不需要判断,可以尝试用 %#v打印变量在golang中的定义获取,如果是打印 []byte类型还可以采用%q%#q,也可以用%T打印变量类型,用%p可以打印指针和切片的地址,判断是否是同一个。

字符串处理的时候注意 rune 用法

// Package stringutil contains utility functions for working with strings.
package stringutil

// Reverse returns its argument string reversed rune-wise left to right.
func Reverse(s string) string {
	r := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

其它

  • ...用法
x := []int{1, 2, 3}
y := []int{4, 5, 6}
x = append(x, y...)
fmt.Println(x)
  • panic 只会 被当前协程defer,不会抛到 main

判断json中是否存在某个字段

type Config struct {
  Site   Site
  Email  *Email
}

c.Email != nil判断即可

单元测试

  • 只测试指定方法

    go test -run="Test指定方法"
    

编译

CGO_ENABLED=0 go build -ldflags '-s -w' xx.go

go test --bench=. -v -test.benchmem

调优
https://studygolang.com/articles/9693
https://www.cnblogs.com/xiaopingfeng/p/9998649.html
@sh7ning
Copy link
Author

sh7ning commented Jan 19, 2021

Golang gc

package main

import (
	"log"
	"runtime"
)

var intMap map[int]int

func main() {
	printMemStats("初始化")

	//mapLen := 10000
	mapLen := 10000000

	// 添加1w个map值
	intMap = make(map[int]int, mapLen)
	for i := 0; i < mapLen; i++ {
		intMap[i] = 1
	}

	printMemStats("增加map数据1")
	// 手动进行gc操作
	runtime.GC()
	// 再次查看数据
	printMemStats("增加map数据后2")

	log.Println("删除前数组长度:", len(intMap))
	for i := 0; i < mapLen; i++ {
		delete(intMap, i)
	}
	log.Println("删除后数组长度:", len(intMap))
	printMemStats("gc前 map数据后")

	// 再次进行手动GC回收
	runtime.GC()
	printMemStats("gc map数据后")

	doGc()
	printMemStats("doGc后")

	// 设置为nil进行回收
	intMap = nil
	runtime.GC()
	printMemStats("设置为nil后")
}

func doGc() {
	nm := make(map[int]int)
	for k, v := range intMap {
		nm[k] = v
	}
	intMap = nm
	runtime.GC()
	printMemStats("dogc runtime gc")
}

func printMemStats(mag string) {
	var m runtime.MemStats
	runtime.ReadMemStats(&m)
	log.Printf("%v:分配的内存 = %vMB, GC的次数 = %v\n", mag, m.Alloc/1024/1024, m.NumGC)
}

@sh7ning
Copy link
Author

sh7ning commented Mar 12, 2021

获取汇编方法:

1. go tool compile -S file.go > file.S  (推荐)
2. go build -gcflags -S file.go 这个需要吧输出拷贝有点麻烦..
3. go tool objdump executable > disassembly

@sh7ning
Copy link
Author

sh7ning commented Apr 7, 2021

私有库:
go list -m -json all
go test -bench=. -benchmem -run=none
go test -bench=. -run=BenchmarkExchange_GenOrder -v
go test -run TestExchange . -v -count=1
go env -w GOPRIVATE="git.yadou.cn"
git config --global url."git@git.yadou.cn:".insteadOf "https://git.yadou.cn/"

protoc -I=/data/workspace/git.yadou.cn/demo/proto --go_out=. --go-grpc_out=. /data/workspace/git.yadou.cn/demo/proto/*.proto

@sh7ning
Copy link
Author

sh7ning commented Jun 2, 2021

go env -w GOPRIVATE="git.demo.com"
git config --global url."git@git.demo.com:".insteadOf "http://git.demo.com/"

[url "git@gitlab.plst.cc:"]
        insteadOf = http://git@git.demo.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment