```
docs(day01): 完善Go语言基础教程内容 - 添加指针概念章节,包含指针的基本概念、使用场景和示例代码 - 补充基础类型章节的零值说明,解释各种基础类型的默认值 - 扩展运算符章节,增加逻辑运算符和位运算符的详细说明 - 更新README目录结构,添加新章节的导航链接 - 添加指针相关的演示图表和示例代码文件 ```
This commit is contained in:
parent
0761778363
commit
243907d045
@ -22,3 +22,4 @@
|
||||
+ [基础类型](./base_type/README.md)
|
||||
+ [变量作用域](./scope/README.md)
|
||||
+ [运算符](./operate/README.md)
|
||||
+ [指针概念](./pointer/README.md)
|
||||
|
||||
@ -155,4 +155,11 @@ const (
|
||||
STATUS_OFFLINE Status = "offline"
|
||||
STATUS_UNKNOW Status = "unknow"
|
||||
)
|
||||
```
|
||||
```
|
||||
|
||||
## 零值
|
||||
|
||||
所有的基础类型,没有被赋值都有一个 零值:
|
||||
整数:0
|
||||
小数:0
|
||||
bool: false
|
||||
|
||||
@ -71,6 +71,12 @@ a, b := 20, 10
|
||||
if a > b {
|
||||
a, b = b, a
|
||||
}
|
||||
|
||||
// if a > b {
|
||||
// //
|
||||
// } else {
|
||||
// ///
|
||||
// }
|
||||
```
|
||||
|
||||
## 逻辑运算符
|
||||
@ -82,6 +88,19 @@ if a > b {
|
||||
+ ! 非(NOT), 如果条件为 True,则逻辑 NOT 条件 False,否则为 True
|
||||
|
||||
```go
|
||||
|
||||
// if age <= 35 && IsX && condtion3 {
|
||||
|
||||
// }
|
||||
|
||||
// online true -> offline
|
||||
// if !online {
|
||||
|
||||
// }
|
||||
// if offline {
|
||||
// }
|
||||
|
||||
|
||||
age := 90
|
||||
gender := "male"
|
||||
|
||||
@ -128,7 +147,7 @@ fmt.Println("a << 2 = %d", a<<2) // 1111 0000 240
|
||||
|
||||
byte 0x17 表示什么? (十进制: 23, 二进制: 0001 0111)
|
||||
|
||||
## 赋值运算符
|
||||
## 赋值运算符(=)
|
||||
|
||||
最基本用法:
|
||||
|
||||
@ -156,6 +175,8 @@ a += 5 // a = a + 5
|
||||
|
||||
## 其他运算符
|
||||
|
||||
指针相关(稍后将)
|
||||
|
||||
+ & 返回变量存储地址
|
||||
+ \* 取出地址对应的值
|
||||
|
||||
|
||||
70
day01/pointer/README.md
Normal file
70
day01/pointer/README.md
Normal file
@ -0,0 +1,70 @@
|
||||
# 指针概念
|
||||
|
||||
|
||||
## 场景
|
||||
|
||||
+ func sum(a, b) total: 这种没有指针参与的
|
||||
+ func sum(a, b, sum): sum 是一个变量, 用于接收函数的处理结果
|
||||
+ fmt.Scan: 接收用户输入的参数
|
||||
+ flag.Parse(): 接收命令行参数
|
||||
+ Restful API Handler(req, *resp): resp 就是变量必须是指针,用于接收 请求处理后的结果
|
||||
|
||||
```go
|
||||
var i int
|
||||
fmt.Println("请输入一个整数:")
|
||||
fmt.Scan(&i) // 把输入写入 i
|
||||
|
||||
var port int
|
||||
flag.IntVar(&port, "port", 8080, "server port") // 命令行参数绑定到变量
|
||||
flag.Parse()
|
||||
```
|
||||
|
||||
|
||||
## 概念
|
||||
|
||||
Go里面的参数 都是值传递, 不是引用传递(指针)
|
||||
|
||||
```go
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func main() {
|
||||
a := 10
|
||||
b := 20
|
||||
c := sum(a, b)
|
||||
println("a + b =", c)
|
||||
|
||||
// 使用指针作为函数参数
|
||||
// 0x14000108020
|
||||
var result int
|
||||
|
||||
// &result (类型安全的指针) 东西叫指针 有类型约束,首GC印象的 地址
|
||||
fmt.Println("result 地址 =", &result)
|
||||
sumWithPointer(a, b, &result)
|
||||
println("a + b =", result)
|
||||
|
||||
// uintptr 是无类型指针,可以存放任意指针的地址值, 裸内存地址, 不安全指针, 可能随时被回事,成为一个无效地址
|
||||
// 非法使用 uintptr 导致程序崩溃
|
||||
fmt.Println("result 地址 uintptr =", uintptr(unsafe.Pointer(&result)))
|
||||
}
|
||||
|
||||
func sum(a int, b int) int {
|
||||
return a + b
|
||||
}
|
||||
|
||||
// 这个函数没有Retrun
|
||||
// *int 就是指针类型, 传递是 int这种类型的变量的地址
|
||||
func sumWithPointer(a int, b int, c *int) {
|
||||
*c = a + b
|
||||
}
|
||||
```
|
||||
|
||||
## 获取用户输入
|
||||
|
||||
|
||||
|
||||
|
||||
## 绑定CLI参数
|
||||
|
||||
49
day01/pointer/main.go
Normal file
49
day01/pointer/main.go
Normal file
@ -0,0 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// a := 10
|
||||
// b := 20
|
||||
// c := sum(a, b)
|
||||
// println("a + b =", c)
|
||||
|
||||
// // 使用指针作为函数参数
|
||||
// // 0x14000108020
|
||||
// var result int
|
||||
|
||||
// // &result (类型安全的指针) 东西叫指针 有类型约束,首GC印象的 地址
|
||||
// fmt.Println("result 地址 =", &result)
|
||||
// sumWithPointer(a, b, &result)
|
||||
// println("a + b =", result)
|
||||
|
||||
// // uintptr 是无类型指针,可以存放任意指针的地址值, 裸内存地址, 不安全指针, 可能随时被回事,成为一个无效地址
|
||||
// // 非法使用 uintptr 导致程序崩溃
|
||||
// fmt.Println("result 地址 uintptr =", uintptr(unsafe.Pointer(&result)))
|
||||
|
||||
// 获取用户输入
|
||||
// var i int
|
||||
// fmt.Println("请输入一个整数:")
|
||||
// fmt.Scan(&i) // 把输入写入 i
|
||||
// fmt.Println("你输入的整数是:", i)
|
||||
|
||||
// 命令行参数解析
|
||||
// --port 9090
|
||||
var port int
|
||||
flag.IntVar(&port, "port", 8080, "server port") // 命令行参数绑定到变量
|
||||
flag.Parse()
|
||||
fmt.Println("服务器端口号是:", port)
|
||||
}
|
||||
|
||||
func sum(a int, b int) int {
|
||||
return a + b
|
||||
}
|
||||
|
||||
// 这个函数没有Retrun
|
||||
// *int 就是指针类型, 传递是 int这种类型的变量的地址
|
||||
func sumWithPointer(a int, b int, c *int) {
|
||||
*c = a + b
|
||||
}
|
||||
133
day01/pointer/pointer.drawio
Normal file
133
day01/pointer/pointer.drawio
Normal file
@ -0,0 +1,133 @@
|
||||
<mxfile host="65bd71144e">
|
||||
<diagram id="bRV9rv3DatMBVeH1xIMn" name="第 1 页">
|
||||
<mxGraphModel dx="797" dy="617" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
|
||||
<root>
|
||||
<mxCell id="0"/>
|
||||
<mxCell id="1" parent="0"/>
|
||||
<mxCell id="2" value="变量(插槽)<div><br></div><div>插槽的地址(编号)</div>" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="340" y="200" width="120" height="70" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="7" style="edgeStyle=none;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;" edge="1" parent="1" source="3" target="2">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="11" value="赋值" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="7">
|
||||
<mxGeometry x="0.2182" y="-1" relative="1" as="geometry">
|
||||
<mxPoint as="offset"/>
|
||||
</mxGeometry>
|
||||
</mxCell>
|
||||
<mxCell id="3" value="值<div>(string)</div>" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="570" y="200" width="120" height="70" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="6" value="分类(类型)<div>string</div>" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="370" y="150" width="60" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="8" value=""bob"" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="600" y="310" width="60" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="12" style="edgeStyle=none;html=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;" edge="1" parent="1" source="9" target="8">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="13" value="变量的值" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="12">
|
||||
<mxGeometry x="-0.2353" y="2" relative="1" as="geometry">
|
||||
<mxPoint as="offset"/>
|
||||
</mxGeometry>
|
||||
</mxCell>
|
||||
<mxCell id="16" style="edgeStyle=none;html=1;exitX=0.5;exitY=0;exitDx=0;exitDy=0;entryX=0.5;entryY=1;entryDx=0;entryDy=0;" edge="1" parent="1" source="9" target="2">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="9" value="name" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="370" y="310" width="60" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="14" value="运算符" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="690" y="80" width="60" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="17" value="a" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="330" y="460" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="23" style="edgeStyle=none;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;" edge="1" parent="1" source="18" target="17">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="18" value="10" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="590" y="460" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="19" value="b" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="330" y="590" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="24" style="edgeStyle=none;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;" edge="1" parent="1" source="20" target="19">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="20" value="20" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="590" y="590" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="21" value="c" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="100" y="530" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="22" value="30" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="590" y="730" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="25" style="edgeStyle=orthogonalEdgeStyle;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=0.442;entryY=1.017;entryDx=0;entryDy=0;entryPerimeter=0;" edge="1" parent="1" source="22" target="21">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="27" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="100" y="960" width="630" height="140" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="29" value="<div style="color: rgb(248, 248, 242); background-color: rgb(39, 40, 34); font-family: &quot;Cascadia Code NF&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace, Menlo, Monaco, &quot;Courier New&quot;, monospace; line-height: 18px; white-space: pre;"><span style="color: #a6e22e;">sumWithPointer</span></div>" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="310" y="920" width="150" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="31" value="result插槽的值: 0" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="150" y="930" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="32" value="a插槽的值: 10" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="450" y="930" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="33" value="b插槽的值 20" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="600" y="930" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="34" value="函数接收的是插槽里面的值(值传递)" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="300" y="860" width="210" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="35" value="c: 30<div>没被使用,等待GC销毁</div>" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="220" y="1020" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="36" value="result插槽的值: 0" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="150" y="830" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="37" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="100" y="1320" width="630" height="140" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="38" value="<div style="color: rgb(248, 248, 242); background-color: rgb(39, 40, 34); font-family: &quot;Cascadia Code NF&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace, Menlo, Monaco, &quot;Courier New&quot;, monospace; line-height: 18px; white-space: pre;"><span style="color: #a6e22e;">sumWithPointer</span></div>" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="310" y="1280" width="150" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="39" value="result插槽地址(编号)" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="150" y="1290" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="40" value="a插槽的值: 10" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="450" y="1290" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="41" value="b插槽的值 20" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="600" y="1290" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="42" value="函数接收的是插槽里面的值(值传递)" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="300" y="1220" width="210" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="48" style="edgeStyle=orthogonalEdgeStyle;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=0.5;entryY=1;entryDx=0;entryDy=0;" edge="1" parent="1" source="43" target="39">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="43" value="30" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="354" y="1380" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="46" style="edgeStyle=none;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" edge="1" parent="1" source="45" target="39">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="47" value="通过 &amp; 获取插槽的地址(编号)" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="46">
|
||||
<mxGeometry x="-0.0857" y="-1" relative="1" as="geometry">
|
||||
<mxPoint as="offset"/>
|
||||
</mxGeometry>
|
||||
</mxCell>
|
||||
<mxCell id="45" value="result 插槽" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="150" y="1160" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
</root>
|
||||
</mxGraphModel>
|
||||
</diagram>
|
||||
</mxfile>
|
||||
Loading…
x
Reference in New Issue
Block a user