owned this note changed 6 years ago
Linked with GitHub

初學 Go 專案目錄結構與設計 - 吳柏毅 (appleboy)

歡迎來到 Modern Web 2019 共筆

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

共筆入口:https://hackmd.io/@ModernWeb/2019
手機版請點選上方 按鈕展開議程列表。

Go in Mediatek

Initial Project with Go in 2018/01

  • Easy to learn
  • Performance
  • Deployment (Drone)

Go Project Layout

Repository folder

  • api/
  • assets/ (Embed files in Go)
  • cmd/
  • configs/
  • docker/
  • pkg/
    • config
    • errors
    • fixtures
  • /.drone.yml (deploy config)
  • /.revive.toml (golint config)
  • /docker-compose.yml (DB, Redis and UI)
  • /Makefile
  • go module config (go.mod and go.sum)
  • /.env.example

前端自己起 Docker 內的 Redis & DB 來測試

Go Module

https://blog.golang.org/using-go-modules

Improve Deployment Using Go Module Proxy

https://github.com/gomods/athens

Save time with proxy: 97s -> 6s

Makefile

不喜歡記一堆 command,記在Makefile中

  • Build
  • Testing
  • Deploy

.env

  • 紀錄控制選項
  • 可以寫在 README.md

Version

  • -X github.com/go-ggz/ggz/pkg/version.Version=$(VERSION)

Assets

API

/healthz

Command line package

  • Golang package: flag
  • urfave/cli
  • spf13/cobra

Config Management

https://github.com/spf13/viper

  • Load config from File
    • _"https://github.com/joho/godotenv/autoload"

/configs

Integration

https://github.com/testcontainers/testcontainers-go

/pkg

/errors
/fixtures
/helper
/middleware
/model gorm or xorm
/module mailer, storage..
/schema
prometheus token

Go Practices

RESTFul api and GraphQL

Model testing

Go testing

go test package_name

  • Test
  • Example
    • output 寫在下面,go 會直接比對
  • Benchmark

Subtest

  • 比如不同 user

Skipping Testing

  • 有些測試在 local 不能測

Parallel

  • 平行測試
tags: MW19
Select a repo