Files
rikako-note/Golang/Golang Document.md
2025-01-07 23:41:43 +08:00

3.1 KiB
Raw Blame History

Golang

Get Started

Enable dependency tracking

当代码对其他module中包含的package进行了import时在自己的module中来管理依赖。

自己的module通过go.mod文件来定义,go.mod文件中会track项目所需要的依赖。

go mod init

go mod init <module-name>命令会创建一个go.mod文件,其中<module-name>会是module path。

在实际开发中module name通常是source code被保存的repository location例如uuidmodule的module name为github.com/google/uuid

go mod tidy

go mod tidy命令会根据import添加缺失的module并且移除未使用的module。

multi-module workspace

示例目录结构如下所示:

  • workspace
  • workspace/hello
  • workspace/example/hello

go work init

在本示例中为了创建多module的workspace可以执行go work init ./hello,其会创建go.work文件,并将./hello目录下的module包含到go.work文件中。

go.work内容如下:

go 1.18

use ./hello

go work use

通过go work use ./example/hello命令,会将./example/hello中的module加入到go.work文件中。

go.work内容如下:

go 1.18

use (
    ./hello
    ./example/hello
)

go work use [-r] [dir]命令行为如下:

  • 如果指定目录存在,会为dirgo.work文件中添加一条use指令
  • 如果指定目录不存在,会删除go.work文件中关于目录的use指令

Gin框架构建restful api

在构建resultful api时通常都会通过json格式来传递数据首先可定义业务实体

// album represents data about a record album.
type album struct {
    ID     string  `json:"id"`
    Title  string  `json:"title"`
    Artist string  `json:"artist"`
    Price  float64 `json:"price"`
}

向response中写入返回数据

可以通过调用gin.ContextIndentedJSON方法来向response中写入数据

// getAlbums responds with the list of all albums as JSON.
func getAlbums(c *gin.Context) {
    c.IndentedJSON(http.StatusOK, albums)
}

解析request中的数据

可以通过gin.ContextBindJSON方法来将请求体中的数据解析到对象中。

// postAlbums adds an album from JSON received in the request body.
func postAlbums(c *gin.Context) {
    var newAlbum album

    // Call BindJSON to bind the received JSON to
    // newAlbum.
    if err := c.BindJSON(&newAlbum); err != nil {
        return
    }

    // Add the new album to the slice.
    albums = append(albums, newAlbum)
    c.IndentedJSON(http.StatusCreated, newAlbum)
}

将请求的endpoint注册到server中

可以将各个请求的处理handler注册到server中并在指定端口上运行server

func main() {
    router := gin.Default()
    router.GET("/albums", getAlbums)
    router.POST("/albums", postAlbums)

    router.Run("localhost:8080")
}

上述示例中,分别向/albums路径注册了GET和POST的处理handler并在localhost:8080上对服务进行监听。