Golang笔记(day1) -- 基本语法1(格式化、iota、条件、循环)

Golang笔记(day1) -- 基本语法1(格式化、iota、条件、循环)

本文档为自学笔记,仅记录部分内容,不覆盖所有语法知识

1. Hello world

package main
import "fmt"
func main() {
	fmt.Println("Hello, World!")
}

2. 格式化字符串

Go 语言中使用 fmt.Sprintffmt.Printf 格式化字符串并赋值给新串:

  • Sprintf 根据格式化参数生成格式化的字符串并返回该字符串。

  • Printf 根据格式化参数生成格式化的字符串并写入标准输出。

package main
import "fmt"
func main() {
	var date = "2023.03.14"
	var time = "16:54"
	fmt.Println(date + " " + time)
	var url = "Date: %s Time: %s"
	var target_url = fmt.Sprintf(url, date, time)
	fmt.Println(target_url)
}
//OUTPUT
2023.03.14 16:54
Date: 2023.03.14 Time: 16:54

3. iota

iota,特殊常量,可以认为是一个可以被编译器修改的常量。

iota 在 const 关键字出现时将被重置为 0(const 内部的第一行之前),const 中每新增一行常量声明将使 iota 计数一次(iota 可理解为 const 语句块中的行索引)。

iota 可以被用作枚举值;

第一个 iota 等于 0,每当 iota 在新的一行被使用时,它的值都会自动加 1;

package main
import "fmt"
func main() {
	const (
		a = 1 << iota
		b = 3 << iota
		c
		d
		e
		f
	)
	fmt.Println(a, b, c, d, e, f)
}
//OUTPUT
1 6 12 24 48 96

4. Type Switch / fallthrough

① switch 语句可以被用于 type-switch 来判断某个 interface 变量中实际存储的变量类型

② 使用 fallthrough 会强制执行后面的 case 语句,fallthrough 不会判断下一条 case 的表达式结果是否为 true。

package main
import "fmt"
func main() {
	var x interface{}
	switch i := x.(type) {
	case nil:
		fmt.Printf(" x 的类型 :%T", i)
	case int:
		fmt.Printf("x 是 int 型")
	case float64:
		fmt.Printf("x 是 float64 型")
	case func(int) float64:
		fmt.Printf("x 是 func(int) 型")
	case bool, string:
		fmt.Printf("x 是 bool 或 string 型")
	default:
		fmt.Printf("未知型")
	}
	println()
	test_fallthrough()
}

func test_fallthrough() {
	switch {
	case false:
		fmt.Println("1、case 条件语句为 false")
		fallthrough
	case true:
		fmt.Println("2、case 条件语句为 true")
		fallthrough
	case false:
		fmt.Println("3、case 条件语句为 false")
		fallthrough
	case true:
		fmt.Println("4、case 条件语句为 true")
	case false:
		fmt.Println("5、case 条件语句为 false")
		fallthrough
	default:
		fmt.Println("6、默认 case")
	}
}
//OUTPUT
 x 的类型 :<nil>
2、case 条件语句为 true
3、case 条件语句为 false
4、case 条件语句为 true

5. select

select 是 Go 中的一个控制结构,类似于 switch 语句。

select 语句只能用于通道操作,每个 case 必须是一个通道操作,要么是发送要么是接收。

select 语句会监听所有指定的通道上的操作,一旦其中一个通道准备好就会执行相应的代码块。

如果多个通道都准备好,那么 select 语句会随机选择一个通道执行。如果所有通道都没有准备好,那么执行 default 块中的代码。

select 语句的语法:

  • 每个 case 都必须是一个通道

  • 所有 channel 表达式都会被求值

  • 所有被发送的表达式都会被求值

  • 如果任意某个通道可以进行,它就执行,其他被忽略。

  • 如果有多个 case 都可以运行,select 会随机公平地选出一个执行,其他不会执行。

否则:

  1. 如果有 default 子句,则执行该语句。

  1. 如果没有 default 子句,select 将阻塞,直到某个通道可以运行;Go 不会重新对 channel 或值进行求值。

package main
import (
	"fmt"
	"time"
)
func main() {
	test1()
	test2()
}

func test1() {
	c1 := make(chan string)
	c2 := make(chan string)
	go func() {
		for i := 0; i < 30; i++ {
			time.Sleep(1 * time.Second)
			c1 <- "one"
		}
	}()
	go func() {
		for i := 0; i < 30; i++ {
			time.Sleep(3 * time.Second)
			c2 <- "two"
		}
	}()
	j := 0
	for i := 0; i < 20; i++ {

		select {
		case msg1 := <-c1:
			fmt.Println("received", msg1, j)
			j++
		case msg2 := <-c2:
			fmt.Println("received", msg2, j)
		}
	}
}

func test2() {
	// 定义两个通道
	ch1 := make(chan string)
	ch2 := make(chan string)
	// 启动两个 goroutine,分别从两个通道中获取数据
	go func() {
		for {
			ch1 <- "from 1"
		}
	}()
	go func() {
		for {
			ch2 <- "from 2"
		}
	}()
	// 使用 select 语句非阻塞地从两个通道中获取数据
	for {
		select {
		case msg1 := <-ch1:
			fmt.Println(msg1)
		case msg2 := <-ch2:
			fmt.Println(msg2)
		default:
			// 如果两个通道都没有可用的数据,则执行这里的语句
			fmt.Println("no message received")
		}
	}
}

6. for

Go 语言的 continue 语句 有点像 break 语句。但是 continue 不是跳出循环,而是跳过当前循环执行下一次循环语句。

for 循环中,执行 continue 语句会触发 for 增量语句的执行。

package main

import "fmt"

func main() {
	test1()
	println()
	test2()
	println()
	test3()
	println()
	//test4()
}

// sample
func test1() {
	strings := []string{"google", "runoob"}
	for i, s := range strings {
		fmt.Println(i, s)
	}

	numbers := [6]int{1, 2, 3, 5}
	for i, x := range numbers {
		fmt.Printf("第 %d 位 x 的值 = %d\n", i, x)
	}
}

// key, value
func test2() {
	map1 := make(map[int]float32)
	map1[1] = 1.0
	map1[2] = 2.0
	map1[3] = 3.0
	map1[4] = 4.0

	// 读取 key 和 value
	for key, value := range map1 {
		fmt.Printf("key is: %d - value is: %f\n", key, value)
	}

	// 读取 key
	for key := range map1 {
		fmt.Printf("key is: %d\n", key)
	}

	// 读取 value
	for _, value := range map1 {
		fmt.Printf("value is: %f\n", value)
	}
}

// for_continue
func test3() {
	/* 定义局部变量 */
	var a int = 10

	/* for 循环 */
	for a < 20 {
		if a == 15 {
			/* 跳过此次循环 */
			a = a + 1
			continue
		}
		fmt.Printf("a 的值为 : %d\n", a)
		a++
	}
}

// 无限循环
func test4() {
	for true {
		fmt.Printf("这是无限循环。\n")
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值