doutong6814 2019-07-24 06:04
浏览 550
已采纳

为什么从代码调用Setenv不起作用?

I have a simple test project which is expected to print goroutine scheduling messages while it is running.

Setting the env from Goland configuration it works. but it doesn't from code like below, the app doesn't print scheduling infomation. Anybody explains what happens ehre?

func TestScheduler1() {

    var wg sync.WaitGroup
    wg.Add(10)

    for i := 0; i < 10; i++ {
        go work(&wg)
    }

    wg.Wait()
    // Wait to see the global run queue deplete.
    time.Sleep(3 * time.Second)
}

func work(wg *sync.WaitGroup) {
    time.Sleep(time.Second)

    var counter int
    for i := 0; i < 1e10; i++ {
        counter++
    }

    wg.Done()
}

init function is like:

 func init()  {
    os.Setenv("GOMAXPROCS", "1")
    os.Setenv("GODEBUG", os.Getenv("GODEBUG") + "schedtrace=1000,scheddetail=1")
}
  • 写回答

1条回答 默认 最新

  • 普通网友 2019-07-24 07:50
    关注

    go command documentation mentions that go examines some environment variables:

    The go command, and the tools it invokes, examine a few different environment variables.

    So, env variables are checked before the program starts, i.e in build time. When you set these variables from code, the program already runs.

    It works from the IDE since the variables are applied before the compilation, therefore the code runs with all the changes you need.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b