[Golang] Create Dockerfile for golang app

Containerizing the application is very popular nowadays and it makes the deployment so easy. This post will show how to create a Dockerfile for golang application. There are basically two steps in the Dockerfile: Build the golang program source code Containerize the golang app binary # Build source code FROM golang:1.18-alpine AS build WORKDIR /go/src/github.com/app COPY . . RUN go mod tidy RUN CGO_ENABLED=0 GOOS=linux go build -a -o main . # Containerize the binary FROM alpine COPY --from=build /go/src/github.com/app/main . EXPOSE 3500 CMD ["/main"] Exporting the port 3500 is because golang app listens to 3500. Here is just an example. ...

[Golang] How to import a package on the specific version

In Golang, when a package is imported, by default the codebase of the package’s default branch will be used by the caller project. However, sometimes we want to import a package’s specific branch codebase. For example, the specific version fixed a bug where we can’t wait until the repository maintainer merges the pull request. So in this post, I will walk you through how to specify a package version in your Golang project. Let me take an example to help explain better. ...

[Golang] Direct request from http to https with reverse proxy

Let’s say we have a reverse proxy. The incoming request is HTTP, the target server hosts HTTPs APIs with self-signed TLS certificates. It is very common that we change the URL host or path with reverse proxy, so that the request can be directed a different API. However, in this case, we also need to add the self-signed certificates in reverse proxy. The below example will show how to achieve it. ...

parse json arguments from command-line flags

[Golang] How to parse the JSON arguments from the Command-line flags

Passing the JSON argument via the Command-line flag seems to be not typical, but we may need it in some cases. This post will show you how to implement it in Golang. The package flag provides an easy way to implement it. Let’s take a look at a simple example of using package flag first: package main import ( "flag" "fmt" ) func main() { var version string // parse the value of the flag setVersion to variable version flag.StringVar(&version, "setVersion", "1.0.0", "") flag.Parse() fmt.Println("version = ", version) } The output is ...

[HackerRank] Simple Explanation: Flatland Space Stations

Introduction Although the problem is categorized as “EASY” in terms of difficulty, I must admit that the solution didn’t immediately come to mind after reading the description. Nevertheless, I managed to find a straightforward and concise approach that is both easy to comprehend and implement. I won’t explain the requirement here, so before reading the below explanation, I highly suggested that making sure we understand this problem’s description. The challenge description can be found https://www.hackerrank.com/challenges/flatland-space-stations/problem ...

[Golang] reflect all

This post is about how to use reflect feature to implement as much as you want to do with Golang. Here are some cases: 1. How to get all the fields’ name of an object? 1type A struct{ 2 Foo string 3 Bar int64 4} 5 6func main() { 7 a := &A{Foo: "afoo"} 8 val := reflect.ValueOf(a).Elem() 9 for i:=0; i<val.NumField();i++{ 10 fmt.Println(val.Type().Field(i).Name) 11 } 12} The output: ...

DigitalOcean Referral Badge
Sign up to get $200, 60-day account credit !