zoukankan      html  css  js  c++  java
  • 我平时用的 golang 项目结构

    写了好多年的 golang,逐渐形成了自己的一套习惯。项目里包含哪些文件,该放的东西放哪,基本都有了套路。当然这也只是针对我自己而已。

    .
    ├── ...  // docker 镜像构建需要忽略的文件
    ├──
    ├── .dockerignore   // docker 镜像构建需要忽略的文件
    ├── Dockerfile  // docker 构建文件
    ├──
    ├── .git
    ├── .gitignore  // git 忽略的文件
    ├──
    ├── .gitlab-ci.yml  // gitlab ci 配置。如果使用的是 github,可以使用 github action
    ├── .golangci.yml   // golangci-lint 使用的针对项目的配置
    ├──
    ├── Makefile    // make 配置文件
    ├──
    ├── dumb-init_1.2.1_amd64   // 构建 docker 镜像时需要
    ├──
    ├── go.mod  // go module 依赖配置
    ├── go.sum
    ├──
    ├── README.md   // 项目 README 文件
    ├── CHANGELOG.md    // 项目的 CHANGELOG
    └── version
    

    Dockerfile 又一个比较好的最佳实践。可供参考:

    # 设置编译使用的 Go SDK 版本。
    ARG GO_VERSION=1.16
    
    # 第一步:编译程序。
    FROM golang:${GO_VERSION} AS builder
    
    # 编译工作目录。
    WORKDIR /src
    
    # 设置 Go Modules 代理。
    ENV GOPROXY=https://goproxy.cn
    
    # 下载项目所需的第三方依赖库。
    COPY ./go.mod ./go.sum ./
    RUN go mod download
    
    # 编译辅助工具。
    RUN go build -o ./gops github.com/google/gops
    
    # 拷贝项目代码并编译。
    COPY ./ ./
    RUN go build -o ./xxx -ldflags "-X main.version=`cat ./version`" github.com/alfred-zhong/xxx
    
    # 第二步:设置运行容器。
    FROM centos:7 AS final
    
    # 设置本地时间。
    RUN mv /etc/localtime /etc/localtime.bak && ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    
    # 程序运行目录。
    RUN mkdir /app
    WORKDIR /app
    
    # 设置运行所需环境变量。
    ENV GOPS=on
    
    # 将编程后生成的可执行文件及相关依赖拷贝进来。
    COPY --from=builder /src/xxx ./
    COPY --from=builder /src/dumb-init_1.2.1_amd64 ./
    COPY --from=builder /src/gops ./
    
    # 设置执行文件权限。
    RUN chmod u+x ./dumb-init_1.2.1_amd64
    
    # 设置暴露端口(需要大于 1024)。
    EXPOSE 28002
    
    # 运行入口。
    CMD ["./dumb-init_1.2.1_amd64", "--", "./xxx"]
    
  • 相关阅读:
    hdoj Last non-zero Digit in N! 【数论】
    spin_lock & mutex_lock的差别?
    20140514,微软5月14日公布8个安全补丁
    教你用笔记本破解无线路由器password
    SSL工作原理
    MS-SQLSERVER中的MSDTC不可用解决方法
    grub2手动引导ubuntu
    用递归翻转一个栈 Reverse a stack using recursion
    腾讯面试
    AngularJS移动开发中的坑汇总
  • 原文地址:https://www.cnblogs.com/snowInPluto/p/14453144.html
Copyright © 2011-2022 走看看