call x.m() is valid if the method set of (the type of) x contains m and the argument list can be assigned to the parameter list of m. If x is addressable and &x’s method set contains m, x.m() is shorthand for (&x).m()" 3
with selectors, a reference to a non-interface method with a value receiver using a pointer will automatically dereference that pointer: pt.Mv is equivalent to (*pt).Mv. As with method calls, a reference to a non-interface method with a pointer receiver using an addressable value will automatically take the address of that value: t.Mp is equivalent to (&t).Mp." 5
переменной item, которая в конце итерации хранит значение последнего элемента Надо: записывать адрес реального значения массива package main import "fmt" func main() { ints := []int{1, 2, 3} intsReferences := make([]*int, len(ints)) for i, _ := range ints { intsReferences[i] = &ints[i] } for _, item := range intsReferences { fmt.Printf("%d ", *item) } } Run 7
for i := 0; i < 3; i++ { defer func() { fmt.Printf("%d ", i) }() } } Run package main import "fmt" func main() { for i := 0; i < 3; i++ { defer fmt.Printf("%d ", i) } } Run 8
цикле - опасный трюк, просьба не повторять в домашних условях!* Докуменация Go: "Each time a "…defer" statement executes, the function value and parameters to the call are evaluated as usual and saved anew but the actual function is not invoked…" *но если очень хочется, то можно 9
string } type User struct{ name string } func (u *User) Name() string { return u.name } func getUserFromDB() *User { return nil // No such user in db so return nil } func getPerson() Person { return getUserFromDB() } func main() { if p := getPerson(); p != nil { fmt.Printf("The person name is %s\n", p.Name()) } } Run 10
(https://golang.org/doc/) Читать интересные статьи, например: "Gotchas of Defer in Go" (https://blog.learngoprogramming.com/gotchas-of-defer-in-go-1-8d070894cb01) "50 Shades of Go" (http://devs.cloudimmunity.com/gotchas-and-common-mistakes-in-go-golang/index.html#slice_data_corruption) Быть внимательными! Ловушки бывают хитро замаскированы. 12
решил 4 задания, а оставшийся приз уходит счастливчику, который решил правильно 3 задания и оказался выбранным генератором случайных чисел. Cсылка на задания доступна в телеграм-канале и группе вконтакте: https://t.me/golang_events_nizhny (https://t.me/golang_events_nizhny) https://vk.com/golang_nizhny ( https://vk.com/golang_nizhny) 13