How to set the "godebug" environment variable from the go file?

1,472 views
Skip to first unread message

Sean

unread,
Dec 16, 2020, 11:50:04 AM12/16/20
to golang-nuts
hi all,
i have a project I have to work with CGo. I want to disable some
controls as they are not good enough right now.
It works when I write "set godebug=cgocheck=0" on Windows in console.
However, this is a GUI program. Console should not open.

I tried this with "os.Setenv". I define it in "func init" it doesn't work.

Something like this:

func init() {
    os.Setenv("godebug", "cgocheck=0")
}

What should I do?

jake...@gmail.com

unread,
Dec 16, 2020, 12:10:36 PM12/16/20
to golang-nuts
Could you explain in more detail why you want to always run with cgocheck=0. That seems ill advised at first glance. I'm not sure what that has to do with "I want to disable some controls."

Ian Lance Taylor

unread,
Dec 16, 2020, 4:30:08 PM12/16/20
to Sean, golang-nuts
At present I believe that the best you can do is something like, in
main (untested):

if os.Getenv("GODEBUG") == "" {
cmd := exec.Command(os.Args[0], os.Args[1:]...)
cmd.Env = append(os.Environ(), "GODEBUG=cgocheck=0")
if err := cmd.Run(); err != nil {
os.Exit(1)
}
os.Exit(0)
}

Ian

Sean

unread,
Dec 18, 2020, 11:19:28 AM12/18/20
to golang-nuts
My previous reply was sent to Ian. Sorry.
The point I don't understand is "os.Args".
I think the parameter here is the name of my program. So like "program.exe".

Can support be added to Golang so that these variables can be accessed
from in-program sources such as "os.Environ"?

Unfortunately I don't know the whole structure of go compiler. I don't
know what problems this can bring.

But not every Golang program works on the console. Some of them are GUIs
and it is not possible to access them from the console.

That would be a great thing.

Ian Davis

unread,
Dec 18, 2020, 11:37:51 AM12/18/20
to golan...@googlegroups.com
On Fri, 18 Dec 2020, at 4:18 PM, Sean wrote:
> My previous reply was sent to Ian. Sorry.
> The point I don't understand is "os.Args".
> I think the parameter here is the name of my program. So like "program.exe".
>

Args is a variable in the os package. Go ensures that the the first element contains the name of the program.

Kurtis Rader

unread,
Dec 18, 2020, 2:03:14 PM12/18/20
to Sean, golang-nuts
On Wed, Dec 16, 2020 at 8:49 AM Sean <s.tolst...@gmail.com> wrote:
The easiest solution is a wrapper script that sets the desired environment variables then launches the target program. I don't use MS Windows but on UNIX like platforms I would create a script like this:

#!/bin/sh
GODEBUG=cgocheck=0 exec /path/to/real/program "$@"

 I would normally rename the target program and install the wrapper script as the name of the target program to make the presence of the wrapper "invisible".

--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

roger peppe

unread,
Dec 20, 2020, 6:29:49 AM12/20/20
to Ian Lance Taylor, Sean, golang-nuts


On Wed, 16 Dec 2020, 21:29 Ian Lance Taylor, <ia...@golang.org> wrote:
On Wed, Dec 16, 2020 at 8:49 AM Sean <s.tolst...@gmail.com> wrote:
>
> hi all,
> i have a project I have to work with CGo. I want to disable some
> controls as they are not good enough right now.
> It works when I write "set godebug=cgocheck=0" on Windows in console.
> However, this is a GUI program. Console should not open.
>
> I tried this with "os.Setenv". I define it in "func init" it doesn't work.
>
> Something like this:
>
> func init() {
>      os.Setenv("godebug", "cgocheck=0")
> }
>
> What should I do?

At present I believe that the best you can do is something like, in
main (untested):

    if os.Getenv("GODEBUG") == "" {
        cmd := exec.Command(os.Args[0], os.Args[1:]...)

It's probably better to use https://golang.org/pkg/os/#Executable rather than os.Args[0] here.

        cmd.Env = append(os.Environ(), "GODEBUG=cgocheck=0")
        if err := cmd.Run(); err != nil {
            os.Exit(1)
        }
        os.Exit(0)
    }

Ian

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcXVReo%2BbBf5ZawwO3QcMUEkRMy5QFxgUbG%2BuOjmmyCT5A%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages