INT 21h

Hi, I am Vladimir Smagin, SysAdmin and Kaptain. Telegram Email / GIT / RSS / GPG

Golang: set variable during build

№ 11467 В разделе Programming от December 12th, 2021,
В подшивках:

Useful to set version or build number. Also you can use embed.

Code:

package main

import "fmt"

var MyVariable = "jopa"

func main() {
	fmt.Printf("%s\n", MyVariable)
}

Build script:

#!/usr/bin/bash

ENVVAR="jopa i piska"

LDFLAGS=(
  "-X 'main.MyVariable=${ENVVAR}'"
)

go build -o testvar -ldflags="${LDFLAGS[*]}" main.go
./testvar

rm -f ./testvar

Output:

$ ./build.sh 
jopa i piska

Нет комментариев »

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Яндекс.Метрика

Fortune cookie: Today's spam: Is your access to porno sites blocked?