WORK AROUND:
Additional discovery. When I 'run' a go program i'm developing,
I use a script:
=====
#!/bin/bash
clear
TRG_PKG='main'
BUILD_TIME=$(date +"%Y%m%d.%H%M%S")
GoVersion=N/A
if [[ $(go version) =~ [0-9]+\.[0-9]+\.[0-9]+ ]];
then
GoVersion=${BASH_REMATCH[0]}
fi
echo Setting $TRG_PKG.BuildTime=$BUILD_TIME
echo Setting $TRG_PKG.GoVersion=$GoVersion
FLAG="-X '$TRG_PKG.BuildTime=$BUILD_TIME'"
FLAG="$FLAG -X '$TRG_PKG.GoVersion=$GoVersion'"
go run -ldflags "$FLAG" $1 $2 $3 $4 $5 $6 $7 $8 $9
exit
====
This sets variables in my program to the build datetime and the
go version used. The -ldflags is what is causing my problem.
It attemps to use any named commandline parameter starting
with a "-" as an -ldflags value, not passing them to my program.
$go_run.sh . --showenv=true "a filename with spaces" "another filename with spaces"
This causes an error.
If I run directly:
$go run . --showenv=true "a filename with spaces" "another filename with spaces"
I get the wanted result:
Args[0]=/tmp/go-build1651259946/b001/exe/myprogram
Args[1]=--showenv=true
Args[2]=a filename with spaces
Args[3]=another filename with spaces
I did notice, when I used these lines of code:
// This picks-off my --showenv parameter from the commandline
flag_showenv := flag.Bool("showenv", false, "some text here")
flag.Parse()
println("flag.CommandLine.Args: " + fmt.Sprintf("%v", flag.CommandLine.Args()))
Unwanted response:
flag.CommandLine.Args: [a filename with spaces another filename with spaces]
// This strips off all named parametes pre-defined in my program.
The un-named parameters are all combined into one string with
no separating mark. :/
So, I guess I will need to use the flag pkg to pick-off the named
parameters on the commandline, and then use os.Args[] to
pick-up the unlabeled commandline parameters.
Now I need to figure out how to set the builddate and goversion
values at run/build/install time.