Golang Notes: Difference between revisions

From Federal Burro of Information
Jump to navigationJump to search
(Created page with "== Questions == import prefixes what ar ethye: * nothing * "." * "_" == Grab bag == === Environment Variables === https://stackoverflow.com/questions/40326540/how-to-ass...")
 
No edit summary
Line 31: Line 31:
}
}
</pre>
</pre>
=== runtime.NumCPU ===
numcpu.go
<pre>
package main
import (
"fmt"
"runtime"
)
func main() {
fmt.Println(runtime.NumCPU())
}
</pre>
go run numcpu.go

Revision as of 20:54, 30 July 2020

Questions

import prefixes what ar ethye:

  • nothing
  • "."
  • "_"

Grab bag

Environment Variables

https://stackoverflow.com/questions/40326540/how-to-assign-default-value-if-env-var-is-empty

func getenv(key, fallback string) string {
    value := os.Getenv(key)
    if len(value) == 0 {
        return fallback
    }
    return value
}
func getEnv(key, fallback string) string {
    if value, ok := os.LookupEnv(key); ok {
        return value
    }
    return fallback
}


runtime.NumCPU

numcpu.go

package main
import (
	"fmt"
	"runtime"
)
func main() {
	fmt.Println(runtime.NumCPU())
}

go run numcpu.go