如何在Golang中检查GOGC值?

We know that the default value for GOGC is 100. But how will get the value from the command prompt. Go env is not printing the GOGC value. how to check the value of GOGC / GC percentage ?

To set the GC target percentage, you hand an GOGC environment variable when running your program. If you set it explicitly, there will be nothing preventing you to obtain it using os.Getenv().

The second option is to use debug.SetGCPercent(percent int) int. As stated in package documentation:

SetGCPercent sets the garbage collection target percentage: a collection is triggered when the ratio of freshly allocated data to live data remaining after the previous collection reaches this percentage. SetGCPercent returns the previous setting. The initial setting is the value of the GOGC environment variable at startup, or 100 if the variable is not set. A negative percentage disables garbage collection.

https://golang.org/pkg/runtime/debug/#SetGCPercent

Thus, you'll get the previous setting of GOGC when you use this function. Here I'm leaving a snippet showing this in work: https://play.golang.org/p/epuPhGXmoyN