Pass Command-line Arguments to Makefile and Go Program


Pass command line arguments to make first, and then pass the arguments again to Go program in Makefile.

The reason to pass the same arguments twice like this is because I use Makefile to manage my workflow so the arguments are passed twice.

Makefile:

target:
      @go run argument.go -argument=$(arg)

Go: argument.go

package main

import (
      "flag"
      "fmt"
)

func main() {
      arg := flag.String("argument", "", "argument from Makefile")
      flag.Parse()

      fmt.Println("argument:", *arg)
}

Example:

$ make target arg=hello
argument: hello

Tested on: Ubuntu Linux 18.04, Go 1.10.2

References:

[1]
[2]makefile - Passing additional variables from command line to make - Stack Overflow
[3][Golang] Parse Command Line Arguments - String Variable