Skip to content

Commit f1f4114

Browse files
author
刘建秋
committed
struct{} update
1 parent d8d0a5a commit f1f4114

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

EN/struct/struct.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,35 @@ This also confirms that many answers often go back to the roots of computer data
2626

2727
For struct types, the order of the fields is critical. If two struct types contain exactly the same fields, but in a different order or are partially merged, then the two structs are different types!
2828

29-
If the struct field starts with a capital letter, then the field is exported (visible outside the package), which also conforms to the visibility rules of the Go language. So a struct can contain both exported and unexported variables.
29+
If the struct field starts with a capital letter, then the field is exported (visible outside the package), which also conforms to the visibility rules of the Go language. So a struct can contain both exported and unexported variables.
30+
31+
32+
### struct{} empty structure
33+
34+
* struct{} features
35+
struct{} is an element free structure type, which is usually used when there is no information storage. The advantage is that the size is 0 and no memory is needed to store values of type struct{}.
36+
* The difference between struct{} and struct{}{}
37+
Struct{}{} is a literal, which constructs a struct{} type value, which is also null.
38+
* Declare as map[string]struct{}
39+
Since struct{} is empty and does not care about the content, the map is transformed into set
40+
Map can obtain whether the key exists through the "comma OK" mechanism, for example_ , OK: = map ["key"], if there is no corresponding value, OK is false.
41+
* Chan struct{}
42+
can be used as exit of channel
43+
* Two structt{}{}}
44+
addresses are equal
45+
46+
```
47+
var set map[string]struct{}
48+
// Initialize the set
49+
set = make(map[string]struct{})
50+
// Add some values to the set:
51+
set["one"] = struct{}{}
52+
set["two"] = struct{}{}
53+
// Check if a value is in the map:
54+
A kind of , ok := set["one"]
55+
//output true
56+
fmt.Println ("Is one in the map?", ok)
57+
A kind of , ok = set["three"]
58+
//output false
59+
fmt.Println ("Is three in the map?", ok)
60+
```

zh_CN/struct/struct.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,37 @@ PHP就是c语言实现的一套高级“程序”语言,只不过是这套“
2727
对于struct类型来说,字段的先后顺序是非常关键的。如果两个struct类型包含了完全相同的字段,但是排列顺序不同或者进行了部分合并,那么这两个struct就是不同的类型!
2828

2929
如果struct字段是大写字母开头,那么该字段就是导出的(包外可见),这也符合Go语言的可见性规则。因此一个struct可以同时包含导出和未导出的变量。
30+
31+
### struct{} 空结构体
32+
33+
* struct{} 特点
34+
struct{}是一个无元素的结构体类型,通常在没有信息存储时使用。优点是大小为0,不需要内存来存储struct {}类型的值。
35+
* struct{} 和 struct{}{} 区别
36+
struct{}{}是一个复合字面量(literal),它构造了一个struct {}类型的值,该值也是空。
37+
* 声明为声明为map[string]struct{}
38+
由于struct{}是空,不关心内容,这样map便改造为set
39+
map可以通过“comma ok”机制来获取该key是否存在,例如_, ok := map["key"],如果没有对应的值,ok为false。
40+
* chan struct{}:可以用作通道的退出
41+
* 两个structt{}{}地址相等
42+
43+
```
44+
var set map[string]struct{}
45+
// Initialize the set
46+
set = make(map[string]struct{})
47+
48+
// Add some values to the set:
49+
set["one"] = struct{}{}
50+
set["two"] = struct{}{}
51+
52+
// Check if a value is in the map:
53+
_, ok := set["one"]
54+
55+
//output true
56+
fmt.Println("Is one in the map?", ok)
57+
_, ok = set["three"]
58+
59+
//output false
60+
fmt.Println("Is three in the map?", ok)
61+
```
62+
63+

0 commit comments

Comments
 (0)