分享
  1. 首页
  2. 文章

Go 语言入门二: 如何编写 Go 代码

xugang · · 1365 次点击 · · 开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

本文是 How to Write Go Code 的学习笔记。通过示范介绍了Go 语言的开发过程和 go 工具的使用。

1. 代码结构

go 语言特点

  • go 开发者通常将所有代码组织在一个工作空间下
  • 一个工作空间包含多个版本控制库(git)
  • 一个版本控制库中包含一个或多个应用包
  • 一个包由多个go 源代码文件组成

1.1 工作空间

工作空间包含三个子目录

  • src 包含所有源代码文件
  • pkg 包含所有包对象,库依赖文件等
  • bin 包含所有可执行文件

go 工具编译源码产生的结果,会存放到 pkg 和 bin 目录。src 目录一般包含多个版本控制库,以追踪多个源码库的开发.

一个典型的 go 工作空间如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
bin/
hello# command executable
outyet# command executable
pkg/
linux_amd64/
github.com/golang/example/
stringutil.a# package object
src/
github.com/golang/example/
.git/# Git repository metadata
hello/
hello.go# command source
outyet/
main.go# command source
main_test.go# test source
stringutil/
reverse.go# package source
reverse_test.go# test source
golang.org/x/image/
.git/# Git repository metadata
bmp/
reader.go# package source
writer.go# package source

示例包含两个版本库(example 和 image),可执行目录和依赖库目录。

1.2 GOPATH 环境变量

GOPATH 环境变量指定 go 工作空间的位置,在开发go 代码前指定。

一般设置在当前用户目录下,如下所示:

1
2
3
4
$mkdir$HOME/work# 创建工作空间目录
export GOPATH=$HOME/work# 指定go 工作空间
export PATH=$PATH:$GOPATH/bin# 将可执行文件目录加入环境变量,方便使用

1.3 导入路径

导入路径是一个包的唯一标识,指定包所在的位置。

导入标准库的包时,指定短导入路径,例如 "fmt", "net/http" 等

导入自定义包时,根据 $GOPATH 相对路径引入

1.4 第一个程序

go 源码放在 $GOPATH/src 路径下。

新建 $GOPATH/src/go_note/src/hello/hello.go 文件,内容如下:

1
2
3
4
5
6
7
8
9
// go "hello world" 示例
packagemain
import"fmt"
func main(){
fmt.Printf("Hello, world.\n")
}

通过 go install 命令编译安装程序:

1
2
$go install go_note/src/hello

go 会找到 $GOPATH/src/go_note/src/hello/ 目录下的源码文件,编译完成将编译结果存放在 $GOPATH/bin 目录下。

执行该程序

1
2
3
$$GOPATH/bin/hello
Hello,world.

1.5 第一个库

新建 $GOPATH/src/go_note/src/stringutil/reverse.go 库文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
// Package stringutil contains utility functions for working with strings.
packagestringutil
// Reverse returns its argument string reversed rune-wise left to right.
func Reverse(sstring)string{
r:=[]rune(s)
fori,j:=0,len(r)-1;i<len(r)/2;i,j=i+1,j-1{
r[i],r[j]=r[j],r[i]
}
returnstring(r)
}

使用 go build 命令进行编译检查

1
2
$go build go_note/src/stringutil

go build 不会输出任何文件,通过 go install 将包对象放入 pkg 目录

确认包编译检查正确后,在 hello.go 文件中使用该包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
packagemain
import(
"fmt"
"go_note/src/stringutil"
)
func main(){
// 输出字符串
fmt.Printf("hello, world\n")
// 反转字符串
fmt.Printf(stringutil.Reverse("\nhello, world"))
}

重新编译安装 hello

1
2
$go install go_note/src/hello

go install 自动引入 stringutil

执行编译后的 hello

1
2
3
4
$$GOPATH/bin/hello
hello,world
dlrow,olleh

此时工作空间结构如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
bin/
hello# command executable
pkg/
darwin_amd64/# this will reflect your OS and architecture
go_note/src/
stringutil.a# package object
src/
go_note/src/
hello/
hello.go# command source
stringutil/
reverse.go# package source

1.6 包名

go 代码的第一条语句是包名声明语句。

1
2
packagename

导入包时,通过包名指定需要导入的包

2. 测试

go 通过 go test 命令和测试包组成了一个轻量测试框架。

创建以 _test.go 结尾,函数名以 TestXXX 形式组成并且包含 func (t *testing.T) 结构的代码文件,测试框架会逐个运行这些函数,如果函数抛出 t.Error 或者 t.Fail 异常,就表示测试不通过。

新建测试文件 $GOPATH/src/go_note/src/stringutil/reverse_test.go,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// reverse library 的测试包
packagestringutil
import"testing"
func TestReverse(t *testing.T){
cases:=[]struct{
in,want string
}{
{"hello, world","dlrow ,olleh"},
{"hello, 世界","界世 ,olleh"},
{"",""},
}
for_,c:=rangecases{
got:=Reverse(c.in)
ifgot!=c.want{
t.Errorf("Reverse(%q) == %q, want %q",c.in,got,c.want)
}
}
}

执行测试工具 go test

1
2
3
$go test go_note/src/stringutil
ok go_note/src/stringutil0.006s

3. 远程包

导入路径描述如何从版本控制系统中过去代码,go 工具通过这个属性,可以获取远程包

go get 获取 https://github.com/golang/example 代码,编译并且安装相应文件

1
2
3
4
$go get github.com/golang/example/hello
$$GOPATH/bin/hello
Hello,Go examples!

参考

@刚刚小码农


有疑问加站长微信联系(非本文作者)

本文来自:ironxu

感谢作者:xugang

查看原文:Go 语言入门二: 如何编写 Go 代码

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信
1365 次点击
暂无回复
添加一条新回复 (您需要 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏