I don't think the Go command is using a shell to parse the value of the -gcflags option.
I believe that the syntax for this option is handled by an internal function called quoted.Split <https://github.com/golang/go/blob/27301e8247580e456e712a07d68890dc1e857000/src/cmd/internal/quoted/quoted.go#L25-L59>, which defines its own syntax for quoting. The result is a slice of string ready to be included as part of the arguments to another command, without any intermediate shell evaluation. If that *is* the syntax gcflags is using (which I'm not 100% sure about), then you can find a corresponding function Join in that same source file which performs the opposite operation: encoding a slice of string as a single string. It's an internal package and so you can't depend on it directly from your program, but its implementation is small so presumably you could copy it into your own program. On Wednesday, November 23, 2022 at 5:09:45 PM UTC-8 chri...@aperture.us wrote: > Hi Kurtis, > > > On Wed, Nov 23, 2022, 4:57 PM Kurtis Rader <kra...@skepticism.us> wrote: > >> On Wed, Nov 23, 2022 at 7:33 AM Christian Stewart <chri...@aperture.us> >> wrote: >> >>> This is what I'm trying to accomplish: >>> >>> Start delve (dlv) with os/exec passing a set of []string os.Args as >>> --build-flags >>> >>> Let's say I have this build arguments set: []string{"-gcflags", "-N -l"} >>> >>> - How can I pass this to --build-flags in Delve? strings.Join(flags, >>> " ") is wrong. >>> >> >> Note that there are two levels of quoting involved in your example and a >> package that does "shell quoting" isn't going to handle this scenario >> correctly except for uninteresting trivial cases. >> > > Please explain? > > >> - How can I pass this to "go" with os/exec correctly? The quotes are >>> not put properly. >>> >> >> I'm not sure I understand that question. If you're asking how to run `go >> -gcflags="-N -l"` via a program written in Go the answer is to call os.exec >> with the literal string "-gcflags=-N -l" as an argument. Note that if you >> execute the `go` command directly, rather than indirectly via a shell, you >> don't need quotes around the "-N -l" portion of that string since a shell >> isn't parsing the command. >> > > That doesn't work, from my tests > > This is why you should avoid running external commands indirectly through >> a shell. Doing so greatly, and usually needlessly, complicates constructing >> the command to be run. >> >> >>> There are some cross-platform cases where the shell quoting is used, >>> and the general "quote something" mechanics are similar on Windows and >>> Linux. >>> >> >> Similar is not the same as identical. The rules for how quoted strings, >> and other punctuation, are handled differs enough that only trivial, >> uninteresting, cases are likely to work correctly on both platforms. Heck, >> even on UNIX like systems if the user has set their login shell to a >> non-POSIX shell (like Fish or Elvish) and your code uses the login shell >> rather than something like /bin/sh you will be in for a surprise. :-) >> > > I'm not calling out to the shell, just passing args to another process > that expects them to be shell quoted. > > Thanks, > Christian > -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/fd94f862-b51f-4746-aa82-3c618ed2e7f8n%40googlegroups.com.