Folder structure
The Go project consists of the following peopleSet meal. We call itcommandhavemain
Identifier.
A project can have
1
packagen
Set meal1
command1
Command and1
packagen
Command and1
package1
Command andn
Set mealn
Command andn
Set meal
Depending on the nature of the project, you need to organize the folder structure in a certain way.
Some real world examples
Let us look at some real examples below. I took them fromFive suggestions for setting up a Go projectWritten by Dave Cheney.
Example with the following items1
package:
Example with the following itemsn
Set meal
Example with the following items1
command
https://github.com/tools/godep
Example with the following items1
Command and1
package
https://github.com/monochromegane/the_platinum_searcher
Example with the following itemsn
Command andn
Set meal
https://github.com/golang/tools
Agreement on file structure within the project
From these examples, we can draw the following conventions:
If you have a command, it can exist inmain.go
The files are placed in the root directory of the project.
Use multiple commands to create onecmd/
Folder, and then create a folder with the command name under this folder. In each folder, ideally.go
A file with the same name will containmain()
Function, although not mandatory.
The package is placed in its own folder, unless you only have one package.
File system location of the installed package
Any Go project can be installed using the following commandgo get
.go get
The package will be installed in the following location$GOPATH/src
. For example, enter
go get github.com/kr/fs
The 1 package repository located at the following location will be downloaded and installedhttps://github.com/kr/fsAnd place it in$GOPATH/src/github.com/kr/fs
. We can use in the programimport github.com/kr/fs
.
What if we type
go get github.com/tools/godep
instead? Remember, this is the single command repository we listed earlier.
go get
Will download it like downloading itgithub.com/kr/fs
And place it in$GOPATH/src
, But since it is a command, it will also compile and create a$GOPATH/bin/godep
Binary.
The Go command is compiled to the name of the folder in which it is located. In this case, the folder is the root folder, so it isgodep
. In the case of using multiple commands,Last subfolderName. Such as idealgithub.com/flaviocopes/tools/cmd/unzip
Will be compiled asunzip
. indeedIs notTo containmain()
Features.
Workspace
Go has a very unique conceptWork area. The workspace is a folder structure,$GOPATH
Pointing. When creating a new workspace, add$GOPATH/bin
Folder and your path. For example, if you want to set the workspace to~/go
(This is the case by default):
export GOPATH=~/go
export PATH=~/go/bin:$PATH
Now all the Go code you run will reference this folder. Despite the above, this allows you to create a separate workspaceIn the official document,
Go programmers usually save all Go code in a workspace.
Please note that this is different from other programming environments, where each project has a separate workspace, and these workspaces are closely related to the version control repository.
This is an example of a workspace, listed in the official documentation:
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
#... (many more repositories and packages omitted) ...
Knowing this, let us see.
Where to place commands and packages
When running the command withgo run
, You can place items wherever you like, but this is a method that is only useful for quick testing.
You should be at$GOPATH/src
Folder with your unique namespace, for example
$GOPATH/src/github.com/flaviocopes/hello
Runtime (from anywhere in the system)
go install github.com/flaviocopes/hello
Go will compile and puthello
Binary input$GOPATH/bin
, Can be run from any location in the system.
The same goes for packagesapart fromWhile runninggo install
, It puts the compiled package in$GOPATH/pkg
.
reference
- https://golang.org/doc/code.html#Organization
- https://dave.cheney.net/2014/12/01/five-suggestions-for-setting-up-a-go-project
- https://skife.org/golang/2013/03/24/go_dev_env.html
- https://stackoverflow.com/questions/14867452/what-is-a-sensible-way-to-layout-a-go-project
- https://medium.com/@benbjohnson/structuring-applications-in-go-3b04be4ff091
More tutorials:
- Use NGINX reverse proxy service Go service
- Copy structure in Go
- Basics of Go web server
- Sorting map types in Go
- In a nutshell
- Go to label description
- Start date and time format
- Use Go for JSON processing
- Variadic function
- Cheat sheet
- Go to the empty interface description
- Use VS Code and Delve to debug Go
- Named Go return parameter
- Generate random numbers and strings in Go
- File system structure of Go project
- Binary search algorithm in Go
- Use command line flags in Go
- GOPATH explained
- Use Go to build a command line application: lolcat
- Use Go to build CLI commands: Cowsay
- Use shell and tube in Go
- Go CLI Tutorial: Fortune Clone
- Use Go to list files in a folder
- Use Go to get a list of repositories from GitHub
- Go, append a short string to the file
- Go, convert the string to byte slices
- Use Go to visualize your local Git contributions
- Getting started with Go CPU and memory analysis
- Solve the "Does not support index" error in the Go program
- Measuring the execution time in the Go program
- Use Go to build a web crawler to detect duplicate titles
- Best Practice: Pointer or Value Receiver?
- Best practice: Should you use methods or functions?
- Go data structure: set
- Go to the map cheat sheet
- Generating the implementation of generic types in Go
- Go data structure: dictionary
- Go data structure: hash table
- Implement event listeners in "through the channel"
- Go data structure: stack
- Go data structure: queue
- Go data structure: binary search tree
- Go data structure: graphics
- Go data structure: linked list
- A complete guide to Go data structures
- Compare Go value
- Is Go object-oriented?
- Use SQL database in Go
- Use environment variables in Go
- Last tutorial: REST API supported by PostgreSQL
- Enable CORS on the Go web server
- Deploy Go application in Docker container
- Why Go is a powerful language to learn as a PHP developer
- Go and delete the io.Reader.ReadString newline
- To start, how to watch the changes and rebuild the program
- To count the months since the date
- Access HTTP POST parameters in Go