Let’s create golang application development environment and project
Goal
Set up golang environment, and create simple project
Env : Mac OS
Install golang and create structure
- Install golang
- Set GOPATH
- Create $HOME/go for Mac
- Create directory structure
Install golang
Go https://go.dev/ and download golang and install
Set GOPATH
For Mac, can add following to .zshrc file
```bash
export GOPATH=${HOME}/go
```
Add GOPATH under User directory/go (please create go directory under HOME)
Create directory structure
Create directory like this
GOPATH/
src/
github.com/
username/
pkg/
bin/
This is expected github repository, if you use different repository, please use your repository url under src/ directory
src | Contains source code (organized by repository path) |
pkg | Stores compiled package files |
bin | Contains compiled binary executables |
Create a project and initialization
As a next step, prepare a project
- Go GOPATH and project target directory under source codes
- Create a project directory
- Run go mod to initialize a project
- (Extra) Get dependency
Go GOPATH and project target directory under source codes
cd ${GOPATH}
cd src/github.com/<username>
Create a project directory
mkdir projectname
cd projectname
projectname is your project name, please use own name
Run go mod to initialize a project
Run following command
go mod init <module-path>
Module path is your module path
This is an example
go mod init github.com/<username>/<projectname>
Indicates project directory structure under GOPATH
(Extra) If you write codes (add import description), and want to update dependency
go get .
コメント