FAQ
Hello Gophers,


I try to write an automatic builder that embeds the build time and version
in the program with the -ldflags -X option, but I have a problem running go
install with the arguments.


My program:

package main
import "fmt"
var (
     BuildTime string
     BuildVersion string
)
func main() {
     fmt.Println("Hello, World :)")
     fmt.Println("Build time:", BuildTime)
     fmt.Println("Build version:", BuildVersion)
}

My builder:

package main
import (
     "fmt"
     "os"
     "os/exec"
     "path/filepath"
     "time"
)
var version = "v1.2.3"
func main() {
     Cmd("go", "install", "program")
     Cmd("program")

     err := os.Remove(filepath.Join(os.ExpandEnv("$GOPATH"), "bin", "program"))
     if err != nil {
         fmt.Println("Remove error:", err)
     }
     Cmd("go", "install", "-ldflags",
         "'-X \"main.BuildTime="+time.Now().Format(time.UnixDate)+
             "\" -X \"main.BuildVersion="+version+"\"'",
         "program")
     Cmd("program")
}
func Cmd(args ...string) {
     fmt.Println("Running:", args)
     cmd := exec.Command(args[0], args[1:]...)
     cmd.Stdout = os.Stdout
     cmd.Stderr = os.Stderr
     err := cmd.Run()
     if err != nil {
         fmt.Println("Error:", err, args)
     }
}

But when I run my builder I only get the following output (emphasis mine):

Running: [go install program]
Running: [program]
Hello, World :)
Build time:
Build version:
Running: [go install -ldflags '-X "main.BuildTime=Thu Sep 3 23:06:44 CEST 2015" -X "main.BuildVersion=v1.2.3"' program]*# program
flag provided but not defined: -X "main.BuildTime
usage: link [options] main.o*
   flags and options...
Error: exit status 2 [go install -ldflags '-X "main.BuildTime=Thu Sep 3 23:06:44 CEST 2015" -X "main.BuildVersion=v1.2.3"' program]
Running: [program]
Error: exec: "program": executable file not found in $PATH [program]

I can run this command without problems on my terminal go install -ldflags
'-X "main.BuildTime=Thu Sep 3 23:06:44 CEST 2015" -X
"main.BuildVersion=v1.2.3"' program but I obviously have some problems in
the argument list in my builder.


Does anyone have an idea how this could be fixed?


Thank you
-Martin

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • James Bardin at Sep 3, 2015 at 11:16 pm
    Try removing the extra single quotes around the -ldflags argument.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Martin Bertschler at Sep 4, 2015 at 12:21 am
    I could have sworn that I already tried that, but that fixed my program.
    Thank you James!

    But then the command failed in my shell:
    $ go install -ldflags -X "main.BuildTime=Fri Sep 4 02:09:39 CEST 2015" -X
    "main.BuildVersion=v1.2.3" exp/program
    can't load package: package main.BuildTime=Fri Sep 4 02:09:39 CEST 2015:
    cannot find package "main.BuildTime=Fri Sep 4 02:09:39 CEST 2015" in any
    of:

    Okay so I just learned something new about the shell... "Inside single
    quotes everything is preserved without exception. You can't even escape
    single quotes."
    <http://stackoverflow.com/questions/13799789/expansion-of-variable-inside-single-quotes-in-a-command-in-bash-shell-script>
    So what happens now as far as I can understand is by using the single
    quotes (that I got from here: Issue 12338
    <https://github.com/golang/go/issues/12338> ) the shell interprets this
    quoted block as a single argument, but because I already put the ldflags
    into one element of my args slice instead of multiple ones, the single
    quotes are unnecessary and make the program fail.

    Does that sound plausible?

    Thanks again for the quick help :)

    -Martin

    On Friday, September 4, 2015 at 1:16:21 AM UTC+2, James Bardin wrote:

    Try removing the extra single quotes around the -ldflags argument.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Martin Bertschler at Sep 4, 2015 at 12:24 am
    I could have sworn that I already tried that, but that fixed my program.
    Thank you James!

    But then the command failed in my shell:
    $ go install -ldflags -X "main.BuildTime=Fri Sep 4 02:09:39 CEST 2015" -X
    "main.BuildVersion=v1.2.3" exp/program
    can't load package: package main.BuildTime=Fri Sep 4 02:09:39 CEST 2015:
    cannot find package "main.BuildTime=Fri Sep 4 02:09:39 CEST 2015" in any
    of:

    Okay so I just learned something new about the shell... "Inside single
    quotes everything is preserved without exception. You can't even escape
    single quotes."
    <http://stackoverflow.com/questions/13799789/expansion-of-variable-inside-single-quotes-in-a-command-in-bash-shell-script>
    So what happens now as far as I can understand is by using the single
    quotes (that I got from here: Issue 12338
    <https://github.com/golang/go/issues/12338> ) the shell interprets this
    quoted block as a single argument, but because I already put the ldflags
    into one element of my args slice instead of multiple ones, the single
    quotes are unnecessary and make the program fail.

    Does that sound plausible?

    Thanks again for the quick help :)

    -Martin

    (Google groups just showed my message twice, so I deleted one but then both
    messages were gone...)
    On Friday, September 4, 2015 at 1:16:21 AM UTC+2, James Bardin wrote:

    Try removing the extra single quotes around the -ldflags argument.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 3, '15 at 9:59p
activeSep 4, '15 at 12:24a
posts4
users2
websitegolang.org

2 users in discussion

Martin Bertschler: 3 posts James Bardin: 1 post

People

Translate

site design / logo © 2021 Grokbase