All Projects → alextanhongpin → go-modules

alextanhongpin / go-modules

Licence: other
Docker + go modules in go1.11

Programming Languages

go
31211 projects - #10 most used programming language
Dockerfile
14818 projects
Makefile
30231 projects

Projects that are alternatives of or similar to go-modules

go-wx-api
微信公众号开发API封装
Stars: ✭ 17 (-26.09%)
Mutual labels:  go-modules
vaingogh
A vanity URL generator for your Go packages.
Stars: ✭ 12 (-47.83%)
Mutual labels:  go-modules
Athens
A Go module datastore and proxy
Stars: ✭ 3,736 (+16143.48%)
Mutual labels:  go-modules
cyclonedx-gomod
Creates CycloneDX Software Bill of Materials (SBOM) from Go modules
Stars: ✭ 27 (+17.39%)
Mutual labels:  go-modules

go-modules with Docker

Project Folder

Create a project outside of $GOPATH, e.g. ~/Documents/<your-project-name>.

$ mkdir ~/Documents/go-modules && cd ~/Documents/go-modules

Init go modules

Init go modules by specifying the project namespace:

$ go mod init <project-namespace>

# E.g.
$ go mod init go-modules

Any packages that you create within this project will have the namespace go-modules.

main

Create a bare main.go file and include this:

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/julienschmidt/httprouter"
)

func main() {
	r := httprouter.New()
	r.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		fmt.Fprint(w, "hello world")
	})
	log.Println("listening to port *:8080. press ctrl + c to cancel.")
	log.Fatal(http.ListenAndServe(":8080", r))
}

pkg

Create a folder called pkg and include a greet package:

// pkg/greet/greet.go
package greet

const Version = 1

Change your main.go file to call the greet package. It should look like this:

package main

import (
	"fmt"
	"log"
	"net/http"

	"go-modules/pkg/greet" // go-modules is our project namespace

	"github.com/julienschmidt/httprouter"
)

func main() {
	r := httprouter.New()
	r.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		fmt.Fprintf(w, "hello world, v%v", greet.Version)
	})
	log.Println("listening to port *:8080. press ctrl + c to cancel.")
	log.Fatal(http.ListenAndServe(":8080", r))
}

Vendor

This command will create a vendor directory locally for external dependencies. This is useful when building the binary (especially in Docker), as you do not need to fetch the dependencies again:

$ go mod vendor

Dockerize

A simplified multi-stage docker build Dockerfile would look like this:

FROM golang:1.11.0-stretch as builder

WORKDIR /go-modules

COPY . ./

# Building using -mod=vendor, which will utilize the v
RUN CGO_ENABLED=0 GOOS=linux go build -mod=vendor -o app 

FROM alpine:3.8

WORKDIR /root/

COPY --from=builder /go-modules/app .

CMD ["./app"]

Refer here for a more complete example.

Run the docker build:

# Build
$ docker build --no-cache -t alextanhongpin/go-modules .

# Run the image
$ docker run -d -p 8080:8080 alextanhongpin/go-modules

# Test
$ curl http://localhost:8080
hello world, v1%
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].