Skip to content

Commit

Permalink
golang go基础撰写
Browse files Browse the repository at this point in the history
  • Loading branch information
wangtunan committed Jul 7, 2024
1 parent affa071 commit fb0108a
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion docs/golang/base/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,67 @@ func main() {
| %t | 布尔值 | bool | 输出true或者false |
| %s | 字符串 | string | 按字符串输出 |
| %q | 字符串 | string | 带引号的字符串输出 |

```go
package main

import "fmt"

func main() {
p1 := "admin"
p2 := 123
p3 := 123.456
p4 := true
fmt.Printf("%%\n")
fmt.Printf("%v\n", 123)
fmt.Printf("%T\n", 123)
fmt.Printf("%p, %p, %p, %p", &p1, &p2, &p3, &p4)

fmt.Println("")
fmt.Println("")

fmt.Printf("%d\n", 123)
fmt.Printf("%b\n", 123)
fmt.Printf("%o\n", 123)
fmt.Printf("%x\n", 123)
fmt.Printf("%X\n", 123)
fmt.Printf("%c\n", 123)

fmt.Println("")
fmt.Println("")

fmt.Printf("%f\n", 123.456)
fmt.Printf("%F\n", 123.456)
fmt.Printf("%.5f\n", 123.456)
fmt.Printf("%e\n", 123.456)
fmt.Printf("%E\n", 123.456)

fmt.Println("")
fmt.Println("")

fmt.Printf("%t\n", true)
fmt.Printf("%s\n", "admin")
fmt.Printf("%q\n", "admin")
}
```

## 运算符
| 运算符 | 分类 | 说明和示例 |
| --- | --- | --- |
| `+, -, *, /` | 算数运算符 | `+`还可用于字符串的拼接,例如:`"hello," + "world"` |
| `++, --` | 算数运算符 | 自增和自减, `num++, num--` |
| `>>, <<` | 位运算符 | 按位整体左移或者右移,例如:`0b1100>>2`变成`0b11``0b1100<<2`变成`0b110000` |
| `&` | 位运算符 | 按位与,对应位置都为1,则1,其它为0,例如:`0b110&0b010` => `0b010` |
| `\|` | 位运算符 | 按位或,对应位置只要有一个1,则1,例如:`0b110\|0b010` => `0b110` |
| `^` | 位运算符 | 按位亦或,对应位置不相同则为1,其它为0,例如:`0b110^0b010` => `0b100` |
| `=, +=, /= ...` | 赋值运算符 | 赋值(简写形式), `num += 1`, 表示 `num = num + 1` |
| `>, >=, <, <=, ==, !=` | 关系运算符 | 判断逻辑关系,`17 >= 18` |
| `&&` | 逻辑运算符 | 且逻辑,只有两个都为真,才为真 |
| `\|\|` | 逻辑运算符 | 或逻辑,只要有一个为真,就为真 |
| `` | 逻辑运算符 | 取反,`!true`变成`false`, `!false`取反变成`true` |
| `&` | 地址运算符 | 取变量的指针地址, `&num` |
| `*` | 地址运算符 | 去指针对应的值, `*pointer` |
| `()`| 优先级运算符 | 提升运算优先级,例如:`a&&(b\|\|c)`,会先计算括号内的 |

## 流程控制语句

Expand Down

0 comments on commit fb0108a

Please sign in to comment.