Golang Notes

From Federal Burro of Information
Jump to navigationJump to search

Questions

import prefixes what are they:

  • 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