Recently, it’s always difficult to remember how to build a basic go-gin api development framework from scratch. Today, let’s record the following.
1. How to start?
With the version update of Go, a new module mechanism has been introduced, which makes me a little confused if I haven't used Go for a period of time. Following the Guide on the official website, I probably understood something. Record the following.
First, create an empty folder and name it as the name of the project, then cd into the folder and use the command:
go mod init xxx.com
The meaning of this command is to initialize the module of your own project and give it a name, and then reference to the internal module of your own project can use the name of this package as a prefix (a little namespace).
2. Introduce the desired package
For example, go's gin
package and fresh
package
go get -u github.com/gin-gonic/gin
go get -u github.com/pilu/fresh
gin
package is a good api development package, which can be used as the basis of an api project.
fresh
package is a hot-loaded package, which allows you to update the modified functions like a front-end development without manual restart.
fresh In ubuntu, you need to add the gopath/bin directory to the environment variable, otherwise you will be prompted that the command cannot be found.
3. Entry file
In go.mod
create the same level called main.go
file, paste the following:
package main
import "fmt"
func main() {
fmt.Print("hello world!")
}
4. Run
fresh
- Test hot reload
Keeping fresh
running state, directly modify hello world!
to Hello world! Check the effect.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。