Go工程管理
go命令大全
Go is a tool for managing Go source code.
Usage:
go <command> [arguments]
The commands are:
bug start a bug report
build compile packages and dependencies
clean remove object files and cached files
doc show documentation for package or symbol
env print Go environment information
fix update packages to use new APIs
fmt gofmt (reformat) package sources
generate generate Go files by processing source
get download and install packages and dependencies
install compile and install packages and dependencies
list list packages or modules
mod module maintenance
run compile and run Go program
test test packages
tool run specified go tool
version print Go version
vet report likely mistakes in packages
Use "go help <command>" for more information about a command.
Additional help topics:
buildconstraint build constraints
buildmode build modes
c calling between Go and C
cache build and test caching
environment environment variables
filetype file types
go.mod the go.mod file
gopath GOPATH environment variable
gopath-get legacy GOPATH go get
goproxy module proxy protocol
importpath import path syntax
modules modules, module versions, and more
module-get module-aware go get
module-auth module authentication using go.sum
module-private module configuration for non-public modules
packages package lists and patterns
testflag testing flags
testfunc testing functions
Use "go help <topic>" for more information about that topic.
代码风格
-
命名
命名规则涉及变量、常量、全局函数、结构、接口、方法等的命名。 Go语言从语法层面进行了以下限定:任何需要对外暴露的名字必须以大写字母开头,不需要对外暴露的则应该以小写字母开头。Go语言明确宣告了拥护骆驼命名法而排斥下划线法 -
格式化工具
go fmt
远程 import 支持
import (
"fmt"
"github.com/myteam/exp/crc32"
)
上面代码引入了一个GitHub托管的包。
然后,在执行go build或者go install之前,只需要加这么一句:
go get github.com/myteam/exp/crc32
文档管理
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
Package foo implements a set of simple mathematical functions. These comments are for
demonstration purpose only. Nothing more.
If you have any questions, please don’t hesitate to add yourself to
golang-nuts@googlegroups.com.
You can also visit golang.org for full Go documentation.
*/
package foo
import "fmt"
// Foo compares the two input values and returns the larger
// value. If the two values are equal, returns 0.
func Foo(a, b int) (ret int, err error) {
if a > b {
return a, nil
} else {
return b, nil
}
return 0, nil
}
// BUG(jack): #1: I'm sorry but this code has an issue to be solved.
// BUG(tom): #2: An issue assigned to another person.
在这段代码里,我们添加了4条注释:版权说明注释、包说明注释、函数说明注释和最后添
加的遗留问题说明。现在我们来提取注释并展示文档,具体代码如下:
go doc foo
工程构建
在正确设置GOPATH环境变量并按规范组织好源代码后,现在我们开始构建工程。当然,用的还是Gotool。我们可以使用go build命令来执行构建,它会在你运行该命令的目录中生成工程的目标二进制文件,而不产生其他结果
go build calc
下一步是将构建成功的包安装到恰当的位置,具体指令如下:
go install calc
跨平台开发
Windows编译Linux的执行文件
Linux编译Windows的执行文件