分类 golang 下的文章

# 使用scratch作为基础镜像
FROM scratch
WORKDIR /app

# 复制本地编译好的应用程序到容器中
# GOOS=linux GOARCH=amd64 CGO_ENABLED=0  go build -o ./bin/main main.go
COPY ./bin/main /app/main

# 复制配置文件和其他必要的文件到容器中
#COPY ./config.toml /app/config.toml

# 复制时区数据文件到容器中 需要把时区文件先复制到项目下
COPY ./zoneinfo/Asia/Shanghai /etc/localtime

# 设置时区环境变量
ENV TZ=Asia/Shanghai

# 运行应用程序
ENTRYPOINT ["/app/main", "-conf", "/app/config.toml"]

package server

import (    
        "embed"
    "github.com/gin-contrib/static"
    "github.com/gin-gonic/gin"
)

//go:embed static
var static embed.FS

func main() {
    r := gin.Default()
        // 创建一个没有顶层目录的FS
    webFS, err := fs.Sub(static, "static")
    if err != nil {
        panic(err)
    }
    r.NoRoute(gin.WrapH(http.FileServer(http.FS(webFS))))
    log.Fatal(r.Run(":8080")

}

错误用法

err := db.Transaction(func(tx *gorm.DB) error {
        tx = tx.Where("tid=?", req.Tid).Delete(Model{})
        if err := tx.Error; err != nil {
            common.LOGGER.Errorf("delete t fail,error:%s", err)
            return fmt.Errorf("删除任务出错")
        }

        if tx.RowsAffected == 0 {
            return fmt.Errorf("操作失败,请检查任务状态")
        }

        err := tx.Where("tid=?", req.Tid).Delete(Result{}).Error
        if err != nil {
            common.LOGGER.Errorf("delete result fail,error:%s", err)
            return fmt.Errorf("删除任务出错")
        }

        return nil
    })

正确用法

err := db.Transaction(func(tx *gorm.DB) error {
        result = tx.Where("tid=?", req.Tid).Delete(Model{})
        if err := result.Error; err != nil {
            common.LOGGER.Errorf("delete t fail,error:%s", err)
            return fmt.Errorf("删除任务出错")
        }

        if result.RowsAffected == 0 {
            return fmt.Errorf("操作失败,请检查任务状态")
        }

        result = tx.Where("tid=?", req.Tid).Delete(Result{})
        if err := result.Error; err != nil {
            common.LOGGER.Errorf("delete result fail,error:%s", err)
            return fmt.Errorf("删除任务出错")
        }

        return nil
    })

来源 https://www.zackwu.com/posts/2021-03-14-why-i-always-get-503-with-golang/
修复

package main

import (
    "crypto/tls"
    "fmt"
    "net/http"
)

func main() {
    defaultCipherSuites := []uint16{0xc02f, 0xc030, 0xc02b, 0xc02c, 0xcca8, 0xcca9, 0xc013, 0xc009, 
            0xc014, 0xc00a, 0x009c, 0x009d, 0x002f, 0x0035, 0xc012, 0x000a}
    client := http.Client{
        Transport: &http.Transport{
            TLSClientConfig: &tls.Config{
                CipherSuites: append(defaultCipherSuites[8:], defaultCipherSuites[:8]...),
            },
        },
    }
    req, err := http.NewRequest("GET", "https://www.bundesregierung.de/breg-en", nil)
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println(err)
        return
    }
    defer resp.Body.Close()
    fmt.Println(resp.Status)
}

// Output:
// 200 OK