目录

Go语言

go语言

1 概述

学习资料

  • Go语言标准包解析 https://syaning.github.io/go-pkgs/time/time.html
  • Go 夜读 https://www.ctolib.com/developer-learning-night-reading-go.html
  • go知识图谱 https://www.processon.com/view/link/5a9ba4c8e4b0a9d22eb3bdf0
  • http://www.opscoder.info/

1.1 goproxy

export GOPROXY=https://goproxy.io
or
go get -u github.com/gpmgo/gopm
gopm get xxx

1.2 grpc

git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc
git clone https://github.com/golang/net.git $GOPATH/src/golang.org/x/net
git clone https://github.com/golang/text.git $GOPATH/src/golang.org/x/text
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
git clone https://github.com/google/go-genproto.git $GOPATH/src/google.golang.org/genproto


cd $GOPATH/src/
go install google.golang.org/grpc

1.3 安装 golang.org/x/*

#!/bin/bash
MODULES="crypto net oauth2 sys text tools"
for module in ${MODULES}
do
    wget https://github.com/golang/${module}/archive/master.tar.gz -O ${GOPATH}/src/golang.org/x/${module}.tar.gz
    cd ${GOPATH}/src/golang.org/x && tar zxvf ${module}.tar.gz && mv ${module}-master/ ${module}
done

1.4 安装 google.golang.org/genproto

wget https://github.com/google/go-genproto/archive/master.tar.gz -O ${GOPATH}/src/google.golang.org/genproto.tar.gz
cd ${GOPATH}/src/google.golang.org && tar zxvf genproto.tar.gz && mv go-genproto-master genproto

2 工具

2.1 go swagger

editor swagger: http://editor.swagger.io/

go get -u github.com/swaggo/swag/cmd/swag
cd $GOPATH/src/github.com/swaggo/swag/cmd/swag
go install

➜  wag
NAME:
   swag - Automatically generate RESTful API documentation with Swagger 2.0 for Go.

USAGE:
   swag [global options] command [command options] [arguments...]

VERSION:
   v1.6.2

COMMANDS:
     init, i  Create docs.go
     help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --help, -h     show help
   --version, -v  print the version
   
   
git clone https://github.com/swagger-api/swagger-ui
编译后的目录可直接接入项目的静态目录下
swagger-ui/dist

cp -r  swagger-ui/dist  $project_path/web/staic/help
#测试go代码
package main

import (
	"flag"
	"log"
	"net/http"
	
)

// @title Rango API
// @version 0.1
// @tag.name decision
// @tag.description decision
// @tag.name action
// @tag.description action
// @tag.name policy
// @tag.description policy
// @description Rango is a powerful and efficient access control system.
// @contact.name API Support
// @contact.email bokun.wang@gmail.com
// host 127.0.0.1:8000
// @BasePath
func main() {
	port := flag.String("p", "8100", "port to serve on")
	directory := flag.String("d", ".", "the directory of static file to host")
	flag.Parse()

	http.Handle("/", http.FileServer(http.Dir(*directory)))

	log.Printf("Serving %s on HTTP port: %s\n", *directory, *port)
	log.Fatal(http.ListenAndServe(":"+*port, nil))
}
  • 最终目录结构
go run main.go -p 80

2.2 swagger语法

参考:https://github.com/swaggo/swag

2.3 go mod

参考 https://roberto.selbach.ca/intro-to-go-modules/

Usage:

	go mod <command> [arguments]

The commands are:

	download    download modules to local cache
	edit        edit go.mod from tools or scripts
	graph       print module requirement graph
	init        initialize new module in current directory
	tidy        add missing and remove unused modules
	vendor      make vendored copy of dependencies
	verify      verify dependencies have expected content
	why         explain why packages or modules are needed
  • go mod init oncall
  • go mod vendor

  • 如果依赖包更新了,需要go get -u $xx@xxx go get -u github.com/wangbokun/go@master 然后可以build,会自动更新go.mod

3 基础语法

3.1 变量

3.2 类型

3.3 流程、条件

3.3.1 select

3.4 函数

3.5 错误处理

3.5.1 defer

3.6 初始化

3.7 匿名组合

3.8 反射

3.9 指针

4 接口

5 并发

6 网络

7 安全