Golang学习笔记//建议的工程组织结构



在golang中有无比强大的Go命令行工具(go build, go run等),消除了工程文件的概念,完全用目录结构和package来推导工程结构和构建顺序,因此golang中的工程组织结构是基于package和目录结构来组织的。这是从《The Way to Go》中摘录的几句,原汁原味:

Packages are the primary means in Go of organizing and compiling code.

Packages are a way to structure code: a program is constructed as a “package”.Every go-file belongs to one (and only one) package (like a library or namespace in other languages). Many  different  .go  files  can  belong  to  one  package,  so  the  filename(s)  and  package  name  are generally not the same.


据此一个典型的工程组织结构是这样的:

< helloword >
	|---<bin> // 尖括号是目录的意思
		helloword.exe
	|---<pkg>
		|---<windows>
			helloword.a
			module1.a
			module2.a
	|---<src>
		|---< helloword >
			helloword.go
			helloword_test.go
		|---<module1>
			add.go
			add_test.go
			sub.go
			sub_test.go
		|---<module2>
			fish.go
			fish_test.go
			cat.go
			cat_test.go
		|---<......>

<src>用于包含所有的源代码,是golang强制要求的。

假设我们需要在helloword.go中使用add.go和fish.go中的函数或变量(package scope),那么只需要在helloword.go中这样写即可:

import (
    M1 "../module1" // M1是别名,自己视情况使用。同时注意../,因为是相对于helloword.go所在目录的。
    M2 "../module2"
)

一个实际的helloword工程(俺没找到如何上传附件,自己敲吧,很简单):


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值