当前位置:网站首页>【Go ~ 0到1 】 第一天 6月24 变量,条件判断 循环语句
【Go ~ 0到1 】 第一天 6月24 变量,条件判断 循环语句
2022-06-28 08:16:00 【秋日的晚霞】
1. 变量的声明方式
标准声明
var 变量名 数据类型 = 变量值
简短声明
变量名 := 变量值
2. if 条件判断
1.if 条件判断
//条件判断:
var flag = true
if flag {
fmt.Println("结果为true")
} else {
fmt.Println("结果为false")
}
2.多条件判断
// 多条件判断
age := 30
if age == 20 {
fmt.Println("年龄刚好是20岁")
} else if age > 20 {
fmt.Println("年龄大于20岁")
} else {
fmt.Println("年龄少于20岁")
}
3.作用域判断
num 的作用域仅限于 if语句中
//作用域判断:
if num := 20; num < 30 {
fmt.Println("num值小于30")
} else {
fmt.Println("num值大于30")
}
3.for 循环语句
1.基本格式
for 变量声明;条件判断;步进表达式{
循环体语句
}
for num := 0; num < 100; num++ {
fmt.Printf("从%d到100\n", num)
}
2.死循环
for ;;{
循环体语句
}
for ; ; {
fmt.Println("我是死循环")
}
end. 练习
统计一段文本中 数字,字母 和 汉字的个数
package main
import (
"bufio"
"fmt"
"io"
"os"
"unicode"
)
var (
//数字 字母 汉字
num, wd, han = 0, 0, 0
)
func main() {
//打开文件
file, err := os.Open("D:\\练习文本.txt")
if err != nil {
fmt.Printf("读取文件出现了错误 %v\n", err.Error())
}
//确保关闭io流
defer file.Close()
//获取字符缓冲流
reader := bufio.NewReader(file)
//转换为string
for {
readString, err := reader.ReadString('\n')
if err == io.EOF {
//说明读取完毕
transition(readString)
fmt.Println("文件读取完毕")
break
}
if err != nil {
fmt.Printf("读取字符流出错 %v\n", err)
}
transition(readString)
}
fmt.Println("数字个数 : ", num, " 字母个数 : ", wd, " 汉字个数 :", han)
}
func transition(readString string) {
for a1, str := range readString {
a1 = a1
fmt.Println(str)
if unicode.IsNumber(str) {
num++
}
if unicode.IsLower(str) {
wd++
}
if unicode.IsUpper(str) {
wd++
}
if unicode.Is(unicode.Han, str) {
han++
}
}
}
边栏推荐
- Installing mysql5.7 under Windows
- Introduction to kubernetes (I)
- js取整的小技巧
- Usage record of Xintang nuc980: self made development board (based on nuc980dk61yc)
- 【学习笔记】拟阵
- Sword finger offer 03 Duplicate number in array
- MySQL implements transaction persistence using redo logs
- js运算符的优先级
- The maximum number of Rac open file descriptors, and the processing of hard check failure
- Redis cerebral fissure
猜你喜欢

MySQL implements transaction persistence using redo logs

Little artist huangxinyang was invited to participate in the Wuhan station of children's unit of Paris Fashion Week

The micro kernel zephyr is supported by many manufacturers!

抗洪救灾,共克时艰,城联优品捐赠10万元爱心物资驰援英德

Kubernetes notes and the latest k3s installation introduction

nlp序列完全可以模拟人脑智能

Devops Basics: Jenkins deployment and use (I)

Prometheus service discovery

Almost Union-Find(带权并查集)
![[learning notes] shortest path + spanning tree](/img/38/42b7e321389e3a47304fca5c37a2cf.png)
[learning notes] shortest path + spanning tree
随机推荐
App automated testing appium tutorial 2 - ADB command
匿名页的反向映射
Prometheus monitoring (I)
npm清理缓存
SQL Master slave Replication Build
[JS] - [throttling and anti shake function]
11grac turn off archive log
Airflow2 configuration windows azure SSO details based on oauth2 protocol
Selenium+chromedriver cannot open Google browser page
Solution: selenium common. exceptions. WebDriverException: Message: ‘chromedriver‘ execu
Redis cluster deployment and application scenarios
[learning notes] differential constraint
Software testing and quality final review
SQL master-slave replication setup
新唐NUC980使用记录:自制开发板(基于NUC980DK61YC)
The RAC cannot connect to the database normally after modifying the scan IP. The ora-12514 problem is handled
Installing MySQL under Linux
js取整的小技巧
Why MySQL cannot insert Chinese data in CMD
【学习笔记】线性基