everfu@MacBook-Pro ~ % go help
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 add dependencies to current module and install them
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
packages package lists and patterns
private configuration for downloading non-public code
testflag testing flags
testfunc testing functions
vcs controlling version control with GOVCS
Use "go help <topic>" for more information about that topic.
声明变量:在 go 中通过 varName := value 的语法声明变量,允许在测试时重要一些值使代码更具可读性
运行一下
bash
go test
PASS
ok efu.me/m 0.193s
switch
go
func Hello(name string, language string) string {
if name == "" {
name = "World"
}
switch language {
case spanish:
helloPrefix = spanishHelloPrefix
case french:
helloPrefix = frenchHelloPrefix
}
return helloPrefix + name
}
完整代码
go
// go 必须有一个 main 包,且必须有一个 main 函数作为程序的入口点
package main
// 导入 go 的 I/O 包
import "fmt"
const spanish = "Spanish"
const french = "French"
const helloPrefix = "Hello, "
const spanishHelloPrefix = "Hola, "
const frenchHelloPrefix = "Bonjour, "
func Hello(name string, language string) string {
if name == "" {
name = "World"
}
return greetingPrefix(language) + name
}
func greetingPrefix(language string) (prefix string) {
switch language {
case spanish:
prefix = spanishHelloPrefix
case french:
prefix = frenchHelloPrefix
default:
prefix = helloPrefix
}
return
}
// main 函数
func main() {
// 打印
fmt.Println(Hello("world", "English"))
}