-
The first need Go installed
-
Create directory for
GOPATHsetting, making a/gofolder and relative subfolders/src,/binand/pkg, as follow:. `-- go\ |-- src\ |-- bin\ `-- pkg\ -
For
GOPATHenvironment variable, add the following lines to the~/.bashrc. TheGOPATHenvirionment variable is set to$HOME/gofor default. However, you can customize it by:export GOPATH=$HOME/<YOUR_PATH>/go export PATH=$GOPATH/bin:$PATH
By command
go envyou can list all set env variables about Go.
-
To create our new project, we need to create a directory for it in
go/src -
Create layout for Go application project by following guide
-
Let's make the project directory the root of a module by using
go mod init:$ go mod init nicomincuzzi/go-webapp
It's common practice to use your github username to keep your project names globally unique, and avoid name conflicts with any of your project dependencies, but you can use any name you like.
-
To add module requirements and sums:
$ go mod tidy
Can open your project with your favorite IDE.
One of the most famous IDE is GoLand IDE, in which you need to flag Enable Go Modules Integration in Preferences... -> Go -> Go Modules
After the installation, Launch VSCode.
Open the Extensions Marketplace (Cmd+Shift+X), search Go and install it.
open the Command Palette (Cmd+Shift+P) and run the Go: Install/Update Tools command.
Install all the Go extensions listed there.
Use the below Go command to install package.
$ go get -u github.com/<PACKAGE_PATH>For example, install gin package:
$ go get -u github.com/gin-gonic/ginIt inserts package in go module (go.mod file) and packages in go/pkg/mod
If you look at the go.mod file, you'll see it now contains this:
module nicomincuzzi/go-webapp
go 1.16
require (
github.com/gin-gonic/gin v1.6.3 // indirect
)You will also see a go.sum file now. This is a text file containing references to the specific versions of all the package dependencies, and their dependencies, along with a cryptographic hash of the contents of that version of the relevant module.
The go.sum file serves a similar function to package-lock.json for a Javascript project and you should always check it into version control along with your source code.
Application dependencies (managed manually or by your favorite dependency management tool like the new built-in Go Modules feature). The go mod vendor command will create the /vendor directory for you.
$ go mod vendor-
Build project
$ go build
or specifying the target folder:
$ go build -o build/hello hello.go
-
Testing
From your project’s root directory, run your first test:
$ go testYou will receive the following output:
Output PASS ok ./math 0.988s
The
go testsubcommand only looks for files with the_test.gosuffix.go testthen scans those file(s) for special functions includingfunc TestXxxand several others that we will cover in later steps.go testthen generates a temporary main package that calls these functions in the proper way, builds and runs them, reports the results, and finally cleans everything up.go testis probably sufficient for our little program, but there will be times when you’ll wish to see what tests are running and how long each takes. Adding the-vflag increases verbosity. Rerun your test with the new flag:$ go test -vYou will see the following output:
Output === RUN TestAdd --- PASS: TestAdd (0.00s) PASS ok ./math 1.410s
Go provides cover tool (out of many built-in tools) to analyze coverage information of a test. We use this tool to accept our coverage profile and outputs an HTML file which contains the human-readable information about the test in a very interactive format.
$ go test -cover$ go test -cover -html=cover.txt -o cover.html -
Run project
$ go run .
We're going to use gin for our web application, which is a lightweight web framework.
Let’s take a quick look at how a request is processed in Gin. The control flow for a typical web application, API server or a microservice looks as follows:
Request -> Route Parser -> [Optional Middleware] -> Route Handler -> [Optional Middleware] -> Response
When a request comes in, Gin first parses the route. If a matching route definition is found, Gin invokes the route handler and zero or more middleware in an order defined by the route definition.
Create a file called main.go containing this code:
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/status", func(c *gin.Context) {
c.String(200, "Healthy!")
})
r.Run(":3030")
}Let's break this down a little:
r := gin.Default()This creates a router object, r, using the built-in defaults that come with gin.
Then, we assign a handler function to be called for any HTTP GET requests to the path /hello, and to return the string "Hello, World!" and a 200 (HTTP OK) status code:
r.GET("/status", func(c *gin.Context) {
c.String(200, "Healthy!")
})Finally, we start our webserver and tell it to listen on port 3000:
r.Run(":3030")To run this code, execute:
$ go run main.goYou should see output like this:
go: finding module for package github.com/gin-gonic/gin
go: found github.com/gin-gonic/gin in github.com/gin-gonic/gin v1.6.3
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /hello --> main.main.func1 (3 handlers)
[GIN-debug] Listening and serving HTTP on :3030Now if you visit http://localhost:3030/status in your web browser, you should see the message "Healthy!"
Everyone is very welcome to contribute to this project. You can contribute just by submitting bugs or suggesting improvements by opening an issue on GitHub.
Distributed under Apache-2.0 License, please see license file within the code for more details.



