手把手教你开始自己第一个Go项目(做项目第一步)
ztj100 2025-07-23 19:25 2 浏览 0 评论
本文永久链接:
https://www.xy1413.com/p/hello_go/
`package xxx is not in std`
我相信很多新手入坑Go都会遇到这个错误。
echo $GOPATH
网上很多教程都提示修改GOPATH或将项目源码链接到$GOPATH所在目录。其实这是不对的,至少现在不需要这样修改。
下面我就手把手教大家开始自己第一个Go项目。
代码组织
- 一个目录就是一个包(package)
- 多个包组成一个模块(module)
- 模块由go.mod定义
一般情况:一个项目由一个模块和多个包组成
代码结构
example1
├── bye
│ └── bye.go
├── go.mod
├── hello
│ └── hello.go
└── main.go
- 项目位置:example1
- 项目有一个模块:由go.mod定义
- 项目有两个包:hello和bye
模块定义
# 注意:这里路径名与模块名的不同
$ cd example1 && go mod init example
$ cat go.mod
module example
go 1.21.4
模块名与代码路径没有绝对关系
导入包
cat main.go
package main
import (
"example/bye"
"example/hello"
"fmt"
"github.com/google/go-cmp/cmp"
)
func main() {
hello.PrintHello()
diff := cmp.Diff("aa bb", "aa cc")
fmt.Printf("%v", diff)
bye.PrintBye()
}
- 导入内置包:import "fmt"
- 导入本地包:import 模块名/包名
- 导入远程包:import "github.com/google/go-cmp/cmp"
下载远程包并生成依赖:
go mod tidy
go: finding module for package github.com/google/go-cmp/cmp
go: downloading github.com/google/go-cmp v0.6.0
go: found github.com/google/go-cmp/cmp in github.com/google/go-cmp v0.6.0
现在go.mod的样子:
cat go.mod
module example
go 1.21.4
require github.com/google/go-cmp v0.6.0
编译
cd example1
# 在本地生成可执行文件 - 注意目标文件名的不同
go build && ./example # 编译当前目录
go build . && ./example # 同上面一样
go build main.go && ./main # 编译指定文件
# 不在本地生成可执行文件
go run example # 运行模块
go run main.go # 运行指定文件
# 安装到$GOPATH - 注意目标文件名的不同
go install . && $GOPATH/bin/example # 安装当前模块
go install example && $GOPATH/bin/example # 安装指定模块
go install main.go && $GOPATH/bin/main # 安装指定文件
测试
cat hello_test.go $? ```
```go
package hello
import (
"example/hello"
"testing"
)
func TestHello(t *testing.T) {
hello.PrintHello()
}
- 测试文件以_test.go结尾
- 测试函数以Test开头
- 只要调用t.Error或t.Fail就表示失败
go test hello_test.go
ok command-line-arguments 0.005s
参考
- How to Write Go Code
版权声明 (c)
相关推荐
- 10条军规:电商API从数据泄露到高可用的全链路防护
-
电商API接口避坑指南:数据安全、版本兼容与成本控制的10个教训在电商行业数字化转型中,API接口已成为连接平台、商家、用户与第三方服务的核心枢纽。然而,从数据泄露到版本冲突,从成本超支到系统崩溃,A...
- Python 文件处理在实际项目中的困难与应对策略
-
在Python项目开发,文件处理是一项基础且关键的任务。然而,在实际项目中,Python文件处理往往会面临各种各样的困难和挑战,从文件格式兼容性、编码问题,到性能瓶颈、并发访问冲突等。本文将深入...
- The Future of Manufacturing with Custom CNC Parts
-
ThefutureofmanufacturingisincreasinglybeingshapedbytheintegrationofcustomCNC(ComputerNumericalContro...
- Innovative Solutions in Custom CNC Machining
-
Inrecentyears,thelandscapeofcustomCNCmachininghasevolvedrapidly,drivenbyincreasingdemandsforprecisio...
- C#.NET serilog 详解(c# repository)
-
简介Serilog是...
- Custom CNC Machining for Small Batch Production
-
Inmodernmanufacturing,producingsmallbatchesofcustomizedpartshasbecomeanincreasinglycommondemandacros...
- Custom CNC Machining for Customized Solutions
-
Thedemandforcustomizedsolutionsinmanufacturinghasgrownsignificantly,drivenbydiverseindustryneedsandt...
- Revolutionizing Manufacturing with Custom CNC Parts
-
Understandinghowmanufacturingisevolving,especiallythroughtheuseofcustomCNCparts,canseemcomplex.Thisa...
- Breaking Boundaries with Custom CNC Parts
-
BreakingboundarieswithcustomCNCpartsinvolvesexploringhowadvancedmanufacturingtechniquesaretransformi...
- Custom CNC Parts for Aerospace Industry
-
Intherealmofaerospacemanufacturing,precisionandreliabilityareparamount.Thecomponentsthatmakeupaircra...
- Cnc machining for custom parts and components
-
UnderstandingCNCmachiningforcustompartsandcomponentsinvolvesexploringitsprocesses,advantages,andcomm...
- 洞察宇宙(十八):深入理解C语言内存管理
-
分享乐趣,传播快乐,增长见识,留下美好。亲爱的您,这里是LearingYard学苑!今天小编为大家带来“深入理解C语言内存管理”...
- The Art of Crafting Custom CNC Parts
-
UnderstandingtheprocessofcreatingcustomCNCpartscanoftenbeconfusingforbeginnersandevensomeexperienced...
- Tailored Custom CNC Solutions for Automotive
-
Intheautomotiveindustry,precisionandefficiencyarecrucialforproducinghigh-qualityvehiclecomponents.Ta...
- 关于WEB服务器(.NET)一些经验累积(一)
-
以前做过技术支持,把一些遇到的问题累积保存起来,现在发出了。1.问题:未能加载文件或程序集“System.EnterpriseServices.Wrapper.dll”或它的某一个依赖项。拒绝访问。解...
你 发表评论:
欢迎- 一周热门
- 最近发表
-
- 10条军规:电商API从数据泄露到高可用的全链路防护
- Python 文件处理在实际项目中的困难与应对策略
- The Future of Manufacturing with Custom CNC Parts
- Innovative Solutions in Custom CNC Machining
- C#.NET serilog 详解(c# repository)
- Custom CNC Machining for Small Batch Production
- Custom CNC Machining for Customized Solutions
- Revolutionizing Manufacturing with Custom CNC Parts
- Breaking Boundaries with Custom CNC Parts
- Custom CNC Parts for Aerospace Industry
- 标签列表
-
- idea eval reset (50)
- vue dispatch (70)
- update canceled (42)
- order by asc (53)
- spring gateway (67)
- 简单代码编程 贪吃蛇 (40)
- transforms.resize (33)
- redisson trylock (35)
- 卸载node (35)
- np.reshape (33)
- torch.arange (34)
- npm 源 (35)
- vue3 deep (35)
- win10 ssh (35)
- vue foreach (34)
- idea设置编码为utf8 (35)
- vue 数组添加元素 (34)
- std find (34)
- tablefield注解用途 (35)
- python str转json (34)
- java websocket客户端 (34)
- tensor.view (34)
- java jackson (34)
- vmware17pro最新密钥 (34)
- mysql单表最大数据量 (35)