doc.dev1x.org

Golang開発環境構築

コンセプト

前提条件

環境

ディレクトリ構成

$tree -a -I .git
.
├── .air.toml
├── .env.example
├── .gitignore
├── Dockerfile
├── README.md
├── docker-compose.yaml
├── go.mod
├── go.sum
└── main.go

Dockerfile

FROM golang:1.16.4

# -- Set Working Dir
WORKDIR /app

# -- Set ENV
ENV CGO_ENABLED=0 \
    GOOS=linux \
    GOARCH=amd64 \
    APP_NAME=XXXXX

# -- install hot reload tool.
RUN curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin

# -- Adding Source
ADD . /app

RUN go build -o ${GOPATH}/bin/${APP_NAME}

# -- Build App
CMD air

docker-compose.yaml

version: '3.8'
services:
  app:
    environment:
      PORT: ${PORT}
    build: .
    volumes:
      - .:/app
    tty: true
    ports:
      - ${PORT}:${PORT}

モジュールの追加

参考資料