Constants

Constants in Go are just that—constant.
can only be numbers, characters (runes), strings or booleans.

enumerated constants are created using the iota enumerator

  1. type ByteSize float64
  2. const (
  3. _ = iota // ignore first value by assigning to blank identifier
  4. KB ByteSize = 1 << (10 * iota)
  5. MB
  6. GB
  7. TB
  8. PB
  9. EB
  10. ZB
  11. YB
  12. )

Variables

  1. var (
  2. home = os.Getenv("HOME")
  3. user = os.Getenv("USER")
  4. gopath = os.Getenv("GOPATH")
  5. )

The init function

Each source file can define its own niladic init function, finally.
(each file can have multiple init functions)

  1. func init() {
  2. if user == "" {
  3. log.Fatal("$USER not set")
  4. }
  5. if home == "" {
  6. home = "/home/" + user
  7. }
  8. if gopath == "" {
  9. gopath = home + "/go"
  10. }
  11. // gopath may be overridden by --gopath flag on command line.
  12. flag.StringVar(&gopath, "gopath", gopath, "override default GOPATH")
  13. }