Golang 构建学习
如何搭建Golang开发环境
1. 下载GOlang包
- https://golang.google.cn/dl/ 在地址上下载Golang
2. 配置包环境
-
修改全局环境变量,GOPROXY,GOPATH,GOROOT
GOPROXY=https://goproxy.cn,direct
GOROOT=“” // go二进制文件的路径 -
mkdir project
创建项目文件 -
go mod init project
创建包模块 -
go get xxxxx
获取第三方包
如何让程序运行起来
// main.go
package main
import "fmt"
func main(){
fmt.Println("hello world")
}
go run main.go
执行程序运行- 程序的入口,要使用
package main
表示为主要入口,入口函数为func main
import "fmt"
在使用包需要引入,如果是自定义的包,则从自己初始化的模块project
开始,例如:go mod init project
fmt.Println
打印日志,包中对外提供的接口,是要求首字母大写来表示的
如何编写函数
func (file *File) Write(b []byte) (n int, err error){} // 1 example
func Write(b []byte) (int, error) {return 0, nil} // 2 example
func nextInt(b []byte, pos int) (value, nextPos int) { return 0, 0} // 3 example
// ---------多值输入-----------------
func MyPrint(words ...string){
for _, w := range words{
fmt.Println(w)
}
}
MyPrint("ab", "c