How to use a golang variable inside exec_command?

Nested1 := func() {
artifact := nested()
log.Println(“Artif:”, artifact)
commands := []string{
“Version=psg **artifact** | cut -d '/' -f16 | cut -d '_' -f2”,
“echo $Version”,
}
exe_cmd(commands)
return
}

  • List item

I need to get the artifact value inside my commands

1 Like

Use fmt.Sprintf like this: https://play.golang.org/p/-s-bxFQqraw

Thank you so much @lutzhorn :slight_smile: I need to return this Version value to this Nested1 function. Can you please help me with that as well :slight_smile:)

Then you should return a value from your func. Did you work through the Go Tour?

@lutzhorn yes I know how to return value to function. But I need to know how to return the shell variable to function?

1 Like

@luthzhorn, i have same dought too bcoz, echo $version is a php syntax which can be interpreted in golang but how?

1 Like

What module is the function exe_command part of? I don’t find it in os/exec. Do you use some 3rd party package? Which?

The package os/exec allows you to connect to the pipes STDIN, STDOUT, and STDERR of the subcommand. Take a look at the example of func (*Cmd) StdoutPipe. This example explains who to read from STDOUT.

See (*Cmd) Output example.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.