今天是一次做Go的笔记,一开始直接打开Github上的Go项目然后跑到Wiki位置,然后作者列出了一堆学习Go的资料,这里我
以第一个学习资料https://tour.golang.org/作为Go学习到入门。然后为了训练我的终端运用
能力还有Vim下的编码能力这里我使用到了tmux
和Vim
编辑器,然后之前已经在VIM里面安装了Vim-go
插件了,所以在之前
的Go项目文件目录下可以直接使用命令模式使用:Go
来执行相应的操作。如果项目报错都话你可能是没有按照
Vim-go
的要求现在项目里面执行:GoInstallBinaries
.
You will also need to install all the necessary binaries. vim-go makes it easy to install all of them by providing a command, :GoInstallBinaries, which will go get all the required binaries.
但是由于但由于 go 的代码很多在 github 和 golang.org 上,涉及到墙的问题。自动安装可能会失败。当然你有梯子的话除外;
我们可以手工安装,进入到GOPATH的SRC目录下,运行命令git clone https://github.com/golang/tools golang.org/x/tools
,再接着上一步:GoInstallBinaries
即可
我的解决方式是先让终端邹代理,至于如何让终端走代理呢,无非就是
export http_proxy=http://127.0.0.1:12333
export http_proxy=https://127.0.0.1:12333
然后在通过命令打开vim,然后再:GoInstallBinaries
,这样子就能下载Go所需要的文件了
最后出现如图所示:
项目一
package main
import (
"fmt"
"math/rand"
)
func main() {
fmt.Println("My favorite number is", rand.Intn(10))
}
然后在Vim下面执行:Go Run
即可输出
My favorite number is 1
项目二 Imports
package main
import (
"fmt"
"math"
)
func main() {
fmt.Printf("Now you have %g problems.\n", math.Sqrt(7))
}
输出
Now you have 2.6457513110645907 problems.
项目三 Exported names
In Go, a name is exported if it begins with a capital letter. For example, Pizza
is an exported name, as is Pi
, which is exported from the math
package.
pizza
and pi
do not start with a capital letter, so they are not exported.
When importing a package, you can refer only to its exported names. Any “unexported” names are not accessible from outside the package.
Run the code. Notice the error message.
To fix the error,