Skip to content

Commit 08cf048

Browse files
taiojiaeveryx
authored andcommitted
feat: add example of Go generics
Update go-version to 1.18 in the builder and module
1 parent 640ec4c commit 08cf048

File tree

8 files changed

+79
-5
lines changed

8 files changed

+79
-5
lines changed

.github/workflows/ci.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ jobs:
1010
runs-on: ubuntu-latest
1111
steps:
1212

13-
- name: Set up Go 1.17
13+
- name: Set up Go 1.18
1414
uses: actions/setup-go@v1
1515
with:
16-
go-version: 1.17
16+
go-version: 1.18
1717

1818
- name: Check out code into the Go module directory
1919
uses: actions/checkout@v2

PROGRESS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 期待您的参与
22

3-
例子已完成与[源项目](https://github.com/mmcgrana/gobyexample) master 分支(截止 20211217 日)的同步,均为 77 个例子。
3+
例子已完成与[源项目](https://github.com/mmcgrana/gobyexample) master 分支(截止 2022319 日)的同步,均为 78 个例子。
44

55
后续如果你发现有更新不及时的情况,或者你觉得目前的翻译内容有任何问题,我们都 `非常欢迎` 各位同学提交 pull request。
66

@@ -27,6 +27,7 @@
2727
- [x] Methods->方法
2828
- [x] Interfaces->接口
2929
- [x] Embedding
30+
- [x] Generics->范型
3031
- [x] Errors->错误处理
3132
- [x] Goroutines->协程
3233
- [x] Channels->通道

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Go Gopher 的版权归 [Renée French](http://reneefrench.blogspot.com/) 所有
8888
1. `tools/serve` 本地预览效果;
8989
1. 通过自测后即可提交 pull request :)
9090

91-
项目现由 [gobyexample-cn](https://github.com/gobyexample-cn) 维护,例子已完全与英文版同步(截止 2021-12-17),均为 77 个,可以在这里查看 [同步进度](PROGRESS.md)
91+
项目现由 [gobyexample-cn](https://github.com/gobyexample-cn) 维护,例子已完全与英文版同步(截止 2022-3-19),均为 78 个,可以在这里查看 [同步进度](PROGRESS.md)
9292

9393
后续可能会出现与英文版同步不及时的情况,`非常欢迎` 各位同学 fork 并提交 pull request。
9494

examples.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Structs->结构体
1919
Methods->方法
2020
Interfaces->接口
2121
Embedding
22+
Generics->范型
2223
Errors->错误处理
2324
Goroutines->协程
2425
Channels->通道

examples/generics/generics.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// 从1.18版本开始,Go添加了对范型的支持,也即类型参数。
2+
3+
package main
4+
5+
import "fmt"
6+
7+
// 作为泛型函数的示例,`MapKeys` 接受任意类型的Map并返回其Key的切片。
8+
// 这个函数有2个类型参数 - `K` 和 `V`;
9+
// `K` 是 `comparable` 类型,也就是说我们可以通过 `==` 和 `!=`
10+
// 操作符对这个类型的值进行比较。这是Go中Map的Key所必须的。
11+
// `V` 是 `any` 类型,意味着它不受任何限制 (`any` 是 `interface{}` 的别名类型).
12+
func MapKeys[K comparable, V any](m map[K]V) []K {
13+
r := make([]K, 0, len(m))
14+
for k := range m {
15+
r = append(r, k)
16+
}
17+
return r
18+
}
19+
20+
// 作为泛型类型的示例, `List` 是一个
21+
// 具有任意类型值的单链表。
22+
type List[T any] struct {
23+
head, tail *element[T]
24+
}
25+
26+
type element[T any] struct {
27+
next *element[T]
28+
val T
29+
}
30+
31+
// 我们可以像在常规类型上一样定义泛型类型的方法
32+
// 但我们必须保留类型参数。
33+
// 这个类型是 `List[T]`,而不是 `List`
34+
func (lst *List[T]) Push(v T) {
35+
if lst.tail == nil {
36+
lst.head = &element[T]{val: v}
37+
lst.tail = lst.head
38+
} else {
39+
lst.tail.next = &element[T]{val: v}
40+
lst.tail = lst.tail.next
41+
}
42+
}
43+
44+
func (lst *List[T]) GetAll() []T {
45+
var elems []T
46+
for e := lst.head; e != nil; e = e.next {
47+
elems = append(elems, e.val)
48+
}
49+
return elems
50+
}
51+
52+
func main() {
53+
var m = map[int]string{1: "2", 2: "4", 4: "8"}
54+
55+
// 当调用范型函数的时候, 我们经常可以使用类型推断。
56+
// 注意,当调用 `MapKeys` 的时候,
57+
// 我们不需要为 `K` 和 `V` 指定类型 - 编译器会进行自动推断
58+
fmt.Println("keys m:", MapKeys(m))
59+
60+
// ... 虽然我们也可以明确指定这些类型。
61+
_ = MapKeys[int, string](m)
62+
63+
lst := List[int]{}
64+
lst.Push(10)
65+
lst.Push(13)
66+
lst.Push(23)
67+
fmt.Println("list:", lst.GetAll())
68+
}

examples/generics/generics.hash

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
6219a4dadc2685ab1b61dc8e95799fd683ccb761
2+
YulcAofh266

examples/generics/generics.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
keys: [4 1 2]
2+
list: [10 13 23]

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module gobyexample
22

3-
go 1.17
3+
go 1.18
44

55
require (
66
github.com/alecthomas/chroma v0.8.2

0 commit comments

Comments
 (0)