use github.com/spf13/viper Get env data
config.go
type urls struct { Demo1 string `mapstructure:"demo1"` Demo2 string `mapstructure:"demo2"` } type NotifyHttps struct { Name string `mapstructure:"name"` Token string `mapstructure:"token"` Urls urls `mapstructure:"urls"` } type env struct { NotifyHttp NotifyHttps `mapstructure:"notify_https"` } .env
notify_https: name: order_comfire token: 123456abcdef urls: demo1: localhost:8888 demo2: localhost:8443 notify.go
refUrls := reflect.ValueOf(config.Env.NotifyHttps.Urls) urlNum := refUrls.NumField() for i := 0; i < urlNum; i++ { url := refUrls.Field(i).String() if url !
https://play.golang.com/p/BPWVd0WAfqR
package main import ( "encoding/json" "fmt" ) var bodyA = []byte(`{ "type": "A", "data": { "name": "Johnny" } }`) var bodyB = []byte(`{ "type": "B", "data": { "nickname": "J." } }`) type TypeA struct { Name string `json:"name"` } type TypeB struct { Nickname string `json:"nickname"` } func main() { req := struct { Type string `json:"type"` Data any `json:"data"` }{} err := json.Unmarshal(bodyA, &req) // bodyB if err !
https://notes.shichao.io/gopl/ch9/
Golang Gin Api become Windows Service Use github.com/kardianos/service
package main import ( "errors" "fmt" "io" "log" "net" "net/http" "os" "path/filepath" "strings" "time" "github.com/gin-gonic/gin" "github.com/joho/godotenv" "github.com/kardianos/service" ) // Process Windows Service // github.com/kardianos/service var logger service.Logger type program struct{} func (p *program) Start(s service.Service) error { // Start should not block. Do the actual work async. go p.run() return nil } func (p *program) Stop(s service.Service) error { // Stop should not block.
https://books.halfrost.com/leetcode/
https://ithelp.ithome.com.tw/articles/10225188
在Windows上編譯
To MacOs
SET CGO_ENABLED=0 SET GOOS=darwin SET GOARCH=amd64 go build main.go To Linux
SET CGO_ENABLED=0 SET GOOS=linux SET GOARCH=amd64 go build main.go To Windows ???
go build main.go 在Linux上編譯
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build main.go CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go