以下笔记摘录自『 The Go Workshop 』。
Golang只有一种文字型别 - String(字串)。分成以下两种表示法:
一、 原始的(raw):一对反引号 『 `` 』 的字串
二、 转译的(interpreted):一对双引号 『 "" 』 的字串
如果想显示的文字里面含有很多的双引号字元、反斜线字元等等,这样会比较推荐使用原始字串,原因如下方范例。
For example,
package main
import "fmt"
func main() {
Example1 := `Please refer to this "https://ithelp.ithome.com.tw/"`
Example2 := `Please refer to this \"https://ithelp.ithome.com.tw/\"`
fmt.Println(Example1)
fmt.Println(Example2)
}
//Outout
Please refer to this "https://ithelp.ithome.com.tw/"
Please refer to this "https://ithelp.ithome.com.tw/"
符文(rune)是一种可以储存单一一个UFT-8字元的型别(Unicode的编码,会占用1~4个位元组)。在Golang中,字串通常都会用UFT-8编码。但以字串这个型别来说,他能储存的文字有也有可能不是UFT-8编码,所以在处理字串时,我们会需要额外的检查。
当文字以string储存时,Golang会以位元(byte)去储存所有的字串,也就是说有些UFT-8字元可能会被拆开成多个位元组。
不论其编码方式是采用单一还是多重位元组,最好是把字串从byte集合转换成rune集合。
package main
import "fmt"
func main() {
username := "Ako sa máš"
for i := 0; i < len(username); i++ {
fmt.Print(username[i], " ")
}
}
//Output
65 107 111 32 115 97 32 109 195 161 197 161
再将位元数值转换成字串看看会发生什麽事。
package main
import "fmt"
func main() {
username := "Ako sa máš"
for i := 0; i < len(username); i++ {
fmt.Print(string(username[i]), " ")
}
}
///Output
A k o s a m ? ?
为了能够安全的处理多位元组编码字串,建议先把byte型别的字串换成rune型别的切片
package main
import "fmt"
func main() {
username := "Ako sa máš"
rune := [] rune(username)
for i := 0; i < len(username); i++ {
fmt.Print(string(username[i]), " ")
}
}
///Output
A k o s a m á š
安装 Django 在 Amazon Linux 2-Day 04 因为鱼类辨识系统会使用到 Pyt...
昨天之後又做了一些尝试: 用php7.4-apache做一个新的容器,然後用docker-php-e...
当我们自己一个人写程序的时候,只要程序码没有写错,排版风格爱怎麽写就怎麽写,什麽时候要换行、什麽时候...
Document Object 我们知道 DOM 是 Document Object Model(文...
最後一位邀请到来自附中资讯科学班的 Tom 来分享~ 影片连结:https://youtu.be/b...