Structs
Go - Composing Structs from Other Structs
Problem: You want a struct that has data and methods of another struct. Solution: Embed an unnamed struct within another struct. The outer struct will ......
Go - Creating One - Time Structs
person := struct { Id int Name string Email string }{ 1 , "Chang Sau Sheong" , "sausheong@email.com" } person = struct { Id int Name string Email stri ......
Go - Parsing Time Displays Into Structs
func main () { str := "4:31am +0800 on Oct 1, 2021" layout := "3:04pm - 0700 on Jan 2, 2006" t , err := time . Parse ( layout , str ) if err != nil { ......
Go - Decoding Data with a Customized Binary Format to Structs
Problem: You want to decode the customized binary format back to structs. Solution: Use the encoding/binary package to take data from the binary forma ......
Go - Decoding gob Format Data to Structs
Problem: You want to decode gob format data back to structs. Solution: Use the encoding/gob package to decode the gob format data back to structs. fun ......
Go - Creating JSON Data Streams from Structs
Problem: You want to create streaming JSON data from structs. Solution: Create an encoder using NewEncoder in the encoding/json package, passing it an ......
Go - Creating JSON Data Byte Arrays from Structs
Problem: You want to create JSON data from a struct. Solution: Create the structs then use the json.Marshal or json.MarshalIndent to marshal the data ......
Go - Parsing JSON Data Streams Into Structs
Problem: You want to parse JSON data from a stream. Solution: Create structs to contain the JSON data. Create a decoder using NewDecoder in the encodi ......
Go - Proverb: Accept interfaces, return structs
fake.go package fake import "fmt" type ExampleStruct struct { Item1 string Item2 string Item3 string } func (e ExampleStruct) LogLine() { fmt.Printf(" ......