From 29dadd85fe91ebc311b51e11cbf3f7949ec1da88 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 3 Jul 2024 02:05:02 +0800 Subject: [PATCH] add concurrency --- Concurrency/Concurrency | 9 ++++++++ Concurrency/Concurrency.go | 5 +++++ strconv/strconv | 3 +++ strconv/strconv.go | 43 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+) create mode 100644 Concurrency/Concurrency create mode 100644 Concurrency/Concurrency.go create mode 100644 strconv/strconv create mode 100644 strconv/strconv.go diff --git a/Concurrency/Concurrency b/Concurrency/Concurrency new file mode 100644 index 0000000..25444cf --- /dev/null +++ b/Concurrency/Concurrency @@ -0,0 +1,9 @@ +并发的介绍 +golang是天然支持高并发的语言 +并发:同一时间内执行多个任务 +并行:同一时刻执行多个任务 + +golang的并发通过goroutine实现,goroutine是用户态的线程,由go的运行时runtime调度,而线程是通过操作系统去调度。 +最终还是放在操作系统的线程去执行,只不过管理(上下文切换)放在用户态,所以开销较小。 + +golang提供channel用来在多个goroutine之间进行通信。 diff --git a/Concurrency/Concurrency.go b/Concurrency/Concurrency.go new file mode 100644 index 0000000..7905807 --- /dev/null +++ b/Concurrency/Concurrency.go @@ -0,0 +1,5 @@ +package main + +func main() { + +} diff --git a/strconv/strconv b/strconv/strconv new file mode 100644 index 0000000..890422d --- /dev/null +++ b/strconv/strconv @@ -0,0 +1,3 @@ +strconv标准库 + +在golang这种强类型语言中,不支持不同类型的数据类型强制转换。 diff --git a/strconv/strconv.go b/strconv/strconv.go new file mode 100644 index 0000000..bbbc89e --- /dev/null +++ b/strconv/strconv.go @@ -0,0 +1,43 @@ +package main + +import ( + "fmt" + "strconv" +) + +func main() { + //从字符串解析出整形 + + str := "10000" + retInt64, err := strconv.ParseInt(str, 10, 64) //均转换成int64 + if err != nil { + fmt.Println(err) + } + fmt.Printf("%v,%T\n", retInt64, retInt64) + + //转换成int类型 + retInt, _ := strconv.Atoi(str) + fmt.Printf("%v,%T\n", retInt, retInt) + + //将数字转换成字符串类型 + + i := int32(97) + ret := fmt.Sprintf("%d", i) + fmt.Printf("%#v\n", ret) + + //数字转换成字符串类型 + iInt := int(i) + retStr := strconv.Itoa(iInt) + fmt.Printf("%#v\n", retStr) + + //字符串转换成bool类型 + boolstr := "true" + boolV, _ := strconv.ParseBool(boolstr) + fmt.Printf("%v,%T\n", boolV, boolV) + + //字符串解析成浮点类型 + floatstr := "1.234" + floatV, _ := strconv.ParseFloat(floatstr, 64) + fmt.Printf("%v,%T\n", floatV, floatV) + +}