Skip to content

Latest commit

 

History

History
58 lines (46 loc) · 925 Bytes

GO.md

File metadata and controls

58 lines (46 loc) · 925 Bytes

Go

Declaration, Initialization & Assignment

  • Group declarations for related constants
const {
  // constants about a...
}

const {
  // constants about b...
}
  • Init successive integer constants using iota [Reference]
const (
  C1 = iota + 1 // offset 1
  _ // skip
  C3
  C4
)

fmt.Println(C1, C3, C4) // "1 3 4"

OOP

  • Implement String() from fmt.Stringer interface for structs in order to log/debug using fmt
type User struct {
  Name  string
  Age int
}

func (u User) String() string {
  return fmt.Sprintf("%s <%s>", u.Name, u.Age)
}

func main() {
  u := User{
      Name:  "Jeff",
      Age: 18,
  }

  fmt.Println(u) // Jeff <18>
}

Numeric

  • Use int64 => time.Duration for time
var delayInSeconds int64 = 15000

// preference
var delayInSeconds time.Duration = 15 * time.Second