Go từ cơ bản đến nâng cao›7. Generics và iteratorQuiz: genericsQuestion 1.`any``comparable``cmp.Ordered``fmt.Stringer`Question 2.Phủ định: mọi kiểu trừ intMọi kiểu có kiểu nền (underlying type) là `int`Số nguyên xấp xỉCon trỏ tới intQuestion 3.`func F[T any](x T) T``type Box[T any] struct{ v T }``func (b Box[T]) Get() T``func (b Box[T]) Map[U any]() Box[U]`Question 4.`return nil``var zero T; return zero``return T{}``return 0`Question 5.`func(yield func(V) bool)``chan V``func() (V, bool)``interface{ Next() V }`Question 6.Go tự dừng goroutine đang chạy iteratorLần gọi `yield` hiện tại trả về `false`; iterator phải `return` ngay — gọi `yield` thêm lần nữa sẽ panic`yield` trả về `true` và các phần tử còn lại bị bỏ qua tự độngKhông được dùng `break` với range-over-funcQuestion 7.Khi muốn duyệt `seq` nhanh hơn `for range`Khi muốn chuyển `seq` thành sliceKhi cần lấy từng phần tử theo nhu cầu, ví dụ duyệt song song hai iterator (Zip); nhớ gọi `stop()` khi xongKhi muốn chạy iterator trên nhiều goroutine song songQuestion 8.Select all correct answers.`slices.Values(s)``maps.Keys(m)``slices.Collect(seq)``slices.Sorted(maps.Keys(m))`Check Mark as completed← PreviousNext →Comments
Comments