mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 06:23:59 +08:00
@@ -9,7 +9,7 @@ var map1 map[keytype]valuetype
|
||||
var map1 map[string]int
|
||||
```
|
||||
|
||||
(`[keytype]`` 和 `valuetype` 之间允许有空格,但是 gofmt 移除了空格)
|
||||
(`[keytype]` 和 `valuetype` 之间允许有空格,但是 gofmt 移除了空格)
|
||||
|
||||
在声明的时候不需要知道 map 的长度,map 是可以动态增长的。
|
||||
|
||||
|
File diff suppressed because one or more lines are too long
@@ -95,8 +95,8 @@ p.i
|
||||
或者:
|
||||
|
||||
```go
|
||||
var mt struct1
|
||||
ms := struct1{10, 15.5, "Chris"}
|
||||
var ms struct1
|
||||
ms = struct1{10, 15.5, "Chris"}
|
||||
```
|
||||
|
||||
混合字面量语法(composite literal syntax)`&struct1{a, b, c}` 是一种简写,底层仍然会调用 `new ()`,这里值的顺序必须按照字段顺序来写。在下面的例子中能看到可以通过在值的前面放上字段名来初始化字段的方式。表达式 `new(Type)` 和 `&Type{}` 是等价的。
|
||||
|
@@ -19,7 +19,7 @@ func NewFile(fd int, name string) *File {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &File(fd, name)
|
||||
return &File{fd, name}
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -43,7 +43,7 @@ func main() {
|
||||
|
||||
// 使用结构体字面量
|
||||
outer2 := outerS{6, 7.5, 60, innerS{5, 10}}
|
||||
fmt.Printf("outer2 is:", outer2)
|
||||
fmt.Println("outer2 is:", outer2)
|
||||
}
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user