You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- func Printf(format string, a ...interface{}) (n int, err error)
+ 和C语言用法几乎一模一样, 只不过新增了一些格式化符号
```go
Expand DownExpand Up
@@ -3616,7 +3622,7 @@ package main
fmt.Println(sce2 == nil) // false
}
```
+ 只声明当没有被创建的切片是不能使用的
+ 只声明但没有被创建的切片是不能使用的
```go
package main
import "fmt"
Expand DownExpand Up
@@ -3776,25 +3782,22 @@ package main
import "fmt"
func main() {
var dict = map[string]string{"name":"lnj", "age":"33", "gender":"male"}
//value, ok := dict["age"]
//if(ok){
// fmt.Println("有age这个key,值为", value)
//}else{
// fmt.Println("没有age这个key,值为", value)
//}
if value, ok := dict["age"]; ok{
fmt.Println("有age这个key,值为", value)
}
}
```go
```
- ***map遍历***
+ 注意: map和数组以及切片不同,map中存储的数据是无序的, 所以多次打印输出的顺序可能不同
```go
```go
var dict = map[string]string{"name":"lnj", "age":"33", "gender":"male"}
for key, value := range dict{
fmt.Println(key, value)
}
```
## 结构体
- Go语言中的结构体和C语言中结构体一样, 都是用来保存一组`不同类型的数据`
- Go语言中的结构体和C语言中结构体一样, 都需要先定义结构体类型再利用结构体类型定义结构体变量
Expand DownExpand Up
@@ -5615,7 +5618,7 @@ func test1() {
panic("异常B")
}
func main() {
test1(10, 0)
test1()
}
```
+ 如果有异常写在defer中, 那么只有defer中的异常会被捕获
Expand All
@@ -5636,7 +5639,7 @@ func test2() {
panic("异常A")
}
func main() {
test1(10, 0)
test2()
}
```
Expand DownExpand Up
@@ -5721,7 +5724,7 @@ func main() {
// 会将字符串先转换为[]rune, 然后遍历rune切片逐个取出传给自定义函数
// 只要函数返回true,代表符合我们的需求, 既立即停止查找
res = strings.IndexFunc("hello 李南江", custom)
fmt.Println(res) // 6
fmt.Println(res) // 4
// 倒序查找`子串`在字符串第一次出现的位置, 找不到返回-1
res := strings.LastIndex("hello 李南江", "l")
Expand Down
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.