一、安装GO
1.1 使用Homebrew安装go环境(如果很慢,可以换个源)
1.2 查看安装信息
主要关注如下输出
1
| GOROOT="/usr/local/Cellar/go/1.10.3/libexec" # 安装目录
|
1.3 配置环境变量
1
| vi ~/.bash_profile # 没有的话会新建一个文件
|
输入如下内容,第一行是安装的目录,第三行是工作目录(可以改成自己喜欢的路径)
1 2 3 4 5
| GOROOT=/usr/local/Cellar/go/1.10.3/libexec export GOROOT export GOPATH=/Users/hisenyuan/golang export GOBIN=$GOPATH/bin export PATH=$PATH:$GOBIN:$GOROOT/bin
|
1.4 让配置文件生效并且查看环境变量
1 2
| source ~/.bash_profile go env # 这时你会发现环境变量已经有改变
|
二、安装GoLand
我是习惯了用jetbrains的idea
发现它家也有go语言的IDE GoLand
于是就去官网下载,安装,找个注册码,修改一下host防止注册码失效
这里就不再累赘了
三、运行需要输入的程序
买了一本《Go语言程序设计》
有些程序需要从标准输入获取信息,然后进行处理,例如dup2
运行之后不知道该肿么办,百度一番之后发现了方法
命令行:Ctl + D
GoLand:command + D
贴一段程序和运行的结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| package main
import ( "bufio" "fmt" "os" )
func main() { counts := make(map[string]int) files := os.Args[1:] if len(files) == 0 { countLines(os.Stdin, counts) } else { for _, arg := range files { f, err := os.Open(arg) if err != nil { fmt.Fprintf(os.Stderr, "dup2: %v\n", err) continue } countLines(f, counts) f.Close() } } for line, n := range counts { if n > 1 { fmt.Printf("%d\t%s\n", n, line) } } } func countLines(f *os.File, counts map[string]int) { input := bufio.NewScanner(f) for input.Scan(){ counts[input.Text()]++ } }
|