Go - Structs - 1

  • Declare type Employee struct {ID int}; var isaac Employee
  • Use . to access field. 
    • Ex. isaac.ID = 123
  • Use & and * to access by pointet
    • Ex. id = &isaac.ID; *id=1+*id
  • Pointer to a struct
    • Ex. var s *Employee = &isaac; s.ID += 1
    • It equals to: var s *Employee; (*s).ID +=1
  • Return struct pointer from a function
    • Ex. func EmployeeByID(id int) *Employee { var found Employee; return *found }; EmployeeByID(1).ID=2
  • Field name will be exported if it begins with a capital letter

沒有留言:

張貼留言

別名演算法 Alias Method

 題目 每個伺服器支援不同的 TPM (transaction per minute) 當 request 來的時候, 系統需要馬上根據 TPM 的能力隨機找到一個適合的 server. 雖然稱為 "隨機", 但還是需要有 TPM 作為權重. 解法 別名演算法...