- Building RESTful Web services with Go
- Naren Yellavula
- 352字
- 2021-07-02 20:14:05
Demystifying GOPATH
GOPATH is nothing but the current appointed workspace on your machine. It is an environment variable that tells the Go compiler about where your source code, binaries, and packages are placed.
The programmers coming from a Python background may know the Virtualenv tool to create multiple projects (with different Python interpreter versions) at the same time. But at a given time, one activates the environment and develops his project. Similarly, you can have any number of Go projects on your machine. While developing, set the GOPATH to one of your projects. The Go compiler now activates that project.
It is a common practice to create a project under the home directory and set the GOPATH environment variable like this:
>mkdir /home/naren/myproject
export GOPATH=/home/naren/myproject
Now we install external packages like this:
go get -u -v github.com/gorilla/mux
Go copies the project called mux into the currently activated project myproject.
For Go get, use the -u flag to install updated dependencies of the external package and -v to see the verbose details of installation.
A typical Go project has the following structure, as mentioned on the official Go website:
Let us understand this structure before digging further:
- bin: Stores the binary of our project; a shippable binary which can be run directly
- pkg: Contains the package objects; a compiled program which supplies package methods
- src: The place for your project source code, tests, and user packages
In Go, all the packages which you import into your main program have an identical structure, github.com/user/project. But who creates all these directories? Should the developer do that? Nope. It is the developer's responsibility to create directories for his/her project. It means he/she only creates the directory src/github.com/user/hello.
When a developer runs the following command, the directories bin and package are created if they did not exist before. .bin consists of the binary of our project source code and .pkg consists of all internal and external packages we use in our Go programs:
go install github.com/user/project
- 社交網(wǎng)絡(luò)對齊
- 數(shù)據(jù)通信網(wǎng)絡(luò)實(shí)踐:基礎(chǔ)知識與交換機(jī)技術(shù)
- Application Development with Qt Creator(Second Edition)
- 計(jì)算機(jī)網(wǎng)絡(luò)與數(shù)據(jù)通信
- 新一代物聯(lián)網(wǎng)架構(gòu)技術(shù):分層算力網(wǎng)絡(luò)
- 物聯(lián)網(wǎng)+BIM:構(gòu)建數(shù)字孿生的未來
- HTML5 Game development with ImpactJS
- 信息通信網(wǎng)絡(luò)建設(shè)安全管理概要2
- IPv6網(wǎng)絡(luò)切片:使能千行百業(yè)新體驗(yàn)
- 物聯(lián)網(wǎng)工程導(dǎo)論(第3版)
- 物聯(lián)網(wǎng)基礎(chǔ)及應(yīng)用
- 商業(yè)的本質(zhì)和互聯(lián)網(wǎng)
- 從物聯(lián)到萬聯(lián):Node.js與樹莓派萬維物聯(lián)網(wǎng)構(gòu)建實(shí)戰(zhàn)
- OSPF協(xié)議原理與功能拓展
- 無線傳感器網(wǎng)絡(luò)定位方法及應(yīng)用