go podcast()

009: Set variables at build time with -LDFLAGS


Listen Later

Usage of -ldflags:

go build -ldflags "-X main.varName=from_build" -o mycli

Inside your code:

var varName string

func main() {
  fmt.Println(varName) // prints "from_build"
}

Here's what I'm using for StaticBackend -v flag:

go build -ldflags \ 
"-X github.com/staticbackendhq/core/config.BuildTime=$(shell date +'%Y-%m-%d.%H:%M:%S') \
-X github.com/staticbackendhq/core/config.CommitHash=$(shell git log --pretty=format:'%h' -n 1) \
-X github.com/staticbackendhq/core/config.Version=$(shell git describe --tags)" \
-o staticbackend

Links:

  • StaticBackend website
  • StaticBackend GitHub repo
  • Build SaaS apps in Go (my online course and book)


...more
View all episodesView all episodes
Download on the App Store

go podcast()By Dominic St-Pierre

  • 3.8
  • 3.8
  • 3.8
  • 3.8
  • 3.8

3.8

6 ratings


More shows like go podcast()

View all
Cup o' Go by Jonathan Hall & Shay Nehmad

Cup o' Go

16 Listeners