- 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
- 5G承載網(wǎng)網(wǎng)絡(luò)規(guī)劃與組網(wǎng)設(shè)計
- Hands-On Chatbot Development with Alexa Skills and Amazon Lex
- 農(nóng)產(chǎn)品物聯(lián)網(wǎng)研究與應(yīng)用
- 新一代物聯(lián)網(wǎng)架構(gòu)技術(shù):分層算力網(wǎng)絡(luò)
- Drush User’s Guide
- 通信簡史:從信鴿到6G+
- 中小型局域網(wǎng)組建、管理與維護實戰(zhàn)
- 計算機網(wǎng)絡(luò)工程實用教程(第2版)
- 6G:面向2030年的移動通信
- 夢工廠之材質(zhì)N次方:Maya材質(zhì)手冊
- 網(wǎng)絡(luò)綜合布線(第2版)
- 轉(zhuǎn)化:提升網(wǎng)站流量和轉(zhuǎn)化率的技巧
- 5G非正交多址接入技術(shù):理論、算法與實現(xiàn)
- 互聯(lián)網(wǎng)心理學:新心理與行為研究的興起
- 智能家庭網(wǎng)絡(luò):技術(shù)、標準與應(yīng)用實踐