Files
DefectingCat.github.io/source/_md/Golang.md
DefectingCat 2385c24169 挖坑&测试
update test

更新文章
1. 测试新主题

挖坑

挖坑

update config

更新文章
1. 简易FaaS平台

更新文章
1. 修复错误

更新文章:Promise信任问题

update theme
1. 合并js

post: update notedly

fix: update faas

feature: change theme

* fix: comment

* feature: pgp

* fix: delete local file

post: update darkmode

update: update dependencies

fix: navbar in post incorrect height
* pre code adapt to dark mode

update new post useCallback

update dependencies

new post tiny router
* add static files

update vue tiny router

添加备案
* 更新依赖

add post

Add ignore file
2021-11-16 20:26:44 +08:00

920 B

修改字符串

字符串底层是由字节组成的切片,但字符串本身是不可变的。修改字符串有两种方法:

  1. 将其转换为 byte 切片
	str := "xfy"
	s := []byte(str)
	s[0] = 'd'
	str = string(s)

	fmt.Println(str)

但这种方法不能处理中文,因为一个中文字符占 3 个 byte。不能只对某一个 byte 进行赋值。

  1. 将其转换为 rune 切片
	str := "小肥羊"
	s := []rune(str)
	s[0] = '大'
	str = string(s)

	fmt.Println(str)

rune 表示单个 Unicode 字符串,是按字符进行处理的,可以修改单个字符。

结构体赋值

给指针字段赋值时,可以省略解引用的星号:

func main() {
	type Cat struct {
		name  string
		age   int
		color string
	}

	myCat := Cat{
		"xfy",
		1,
		"cows",
	}

	fmt.Println(myCat)

	var c3 *Cat = new(Cat)
	c3.name = "test" // Equal to (*c3).name = "test"
}