摘要:
Problem: You want to create a queue data structure. Solution: Wrap a struct around a slice. Create queue functions on the struct. A queue is a first-i
阅读全文
posted @ 2023-10-08 16:00
ZhangZhihuiAAA
阅读(10)
推荐(0)
摘要:
Problem: You want to sort a map by its keys. Solution: Get the keys of the map in a slice and sort that slice. Then, using the sorted slice of keys, i
阅读全文
posted @ 2023-10-08 15:35
ZhangZhihuiAAA
阅读(13)
推荐(0)
摘要:
Problem: You want to sort elements in an array or slice. Solution: For int , float64 , and string arrays or slices you can use sort.Ints , sort.Float6
阅读全文
posted @ 2023-10-08 10:50
ZhangZhihuiAAA
阅读(38)
推荐(0)
摘要:
Problem: You want to make arrays and slices safe for concurrent use by multiple goroutines. Solution: Use a mutex from the sync library to safeguard t
阅读全文
posted @ 2023-10-08 10:19
ZhangZhihuiAAA
阅读(19)
推荐(0)
摘要:
To take out the first element of the slice: numbers := []int{3, 14, 159, 26, 53, 58} numbers = numbers[1:] // remove element 0 To take out the last el
阅读全文
posted @ 2023-10-07 22:58
ZhangZhihuiAAA
阅读(12)
推荐(0)
摘要:
There is no built-in function for insertion, but you can still use append for the task. Let’s say you want to insert the number 1000 between elements
阅读全文
posted @ 2023-10-07 22:50
ZhangZhihuiAAA
阅读(30)
推荐(0)
摘要:
Problem: You want to define metadata to describe the struct fields. Solution: Use struct tags to define metadata and the reflect package to access the
阅读全文
posted @ 2023-10-07 22:32
ZhangZhihuiAAA
阅读(18)
推荐(0)
摘要:
Problem: You want to create an instance of a struct. Solution: Create a struct instance directly using the name of the struct, or a pointer to a struc
阅读全文
posted @ 2023-10-07 11:28
ZhangZhihuiAAA
阅读(8)
推荐(0)
摘要:
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 { log.Print
阅读全文
posted @ 2023-10-06 13:13
ZhangZhihuiAAA
阅读(12)
推荐(0)
摘要:
func main() { t := time.Now() fmt.Println(t.Format("3:04PM")) fmt.Println(t.Format("Jan 02, 2006")) } When you run this you should see something like:
阅读全文
posted @ 2023-10-06 12:54
ZhangZhihuiAAA
阅读(16)
推荐(0)