Go Commentary #7: Releases, Websockets, and Struct Behavior
Go 1.23 Release Note
- Full notes that you should skim through to get full-fledged of Go 1.23
A New Home for nhooyr/websocket
- nhooyr/websocket is adopted by Coder, CDE - Cloud Development Environment
Go structs are copied on assignment
- Inspired by Common Go Mistakes
type Thing struct {
Name string
}
func main() {
thing := Thing{"record"}
other_thing := thing
other_thing.Name = "banana"
fmt.Println(thing) // {record}
}
type Thing struct {
Name string
}
func findThing(things []Thing, name string) *Thing {
for _, thing := range things {
if thing.Name == name {
return &thing
}
}
return nil
}
func main() {
things := []Thing{Thing{"record"}, Thing{"banana"}}
thing := findThing(things, "record")
thing.Name = "gramaphone"
fmt.Println(things) // [{record} {banana}]
}
=> fix:
func findThing(things []Thing, name string) *Thing {
for i := range things {
if things[i].Name == name {
return &things[i]
}
}
return nil
}
func main() {
x := []int{1, 2, 3, 4, 5}
y := x[2:3]
fmt.Println(y)
y = append(y, 555) // y = {3, 555}
fmt.Println(x) // {1, 2, 3, 555, 5}
}
=> fix:
func main() {
x := []int{1, 2, 3, 4, 5}
y := x[2:3:3]
fmt.Println(y)
y = append(y, 555) // y = {3, 555}
fmt.Println(x) // {1, 2, 3, 4, 5}
}