package main
import "fmt"
import "strings"
func main(){
char := 'A'
fmt.Printf("类型: %T, 值: %d\n", char, char)
str := "A"
fmt.Printf("类型: %T, 值: %s\n", str, str)
raw := `Line 1\nLine 2`
fmt.Println(raw)
testIota()
testIf()
testSwith()
testFor()
testSliceForRang()
testSlice()
testString()
}
func testIota(){
const (
a = iota * 2
b
c = 100
d = iota * 2
e
)
fmt.Println(a,b,c,d,e)
}
func testIf(){
x := 1;
if (x<=1){
fmt.Println("真小于等于1")
} else {
fmt.Println("大于1")
}
}
func testSwith() {
num := f()
switch {
case num >= 0 && num <= 1:
num++
case num > 1:
num--
fallthrough
case num == 2:
num += num
default:
num = 0
}
fmt.Println("num = ", num);
}
func f() int {
return 3
}
func testFor() {
Out:
for i := 0; i <= 5; i++ {
for j := 0; j <= 5; j++ {
if i > j {
fmt.Println("i = ", i)
continue Out
}
fmt.Println(i, j)
}
}
}
func testSliceForRang() {
slice := []int{1, 2, 3, 4, 5, 7, 8, 9}
for index, val := range slice {
fmt.Println(index, val)
}
}
func testSlice() {
num1,num2 := make([]int,3,5),[]string{"apple", "origan"}
num1[0], num1[1], num1[2] = 1, 2, 3
num1len,num2len := len(num1), len(num2)
num1cap,num2cap := cap(num1),cap(num2)
slice1 := num1[:2]
slice2 := append(num1[2:],num1[:2]...)
fmt.Println("切片1:", num1, " 长度:", num1len, "容量:",num1cap)
fmt.Println("切片2:", num2, " 长度:", num2len, "容量:",num2cap)
fmt.Println("slice1", slice1)
fmt.Println("slice2", slice2)
}
func testString() {
str := "这是一个字符!"
runes1 := []rune(str)
bytes := []byte(str)
fmt.Println("runes1:",runes1)
fmt.Println("bytes:",bytes)
fmt.Println("string(runes1):",string(runes1))
fmt.Println("string(bytes):",string(bytes))
var dst1,src,_ string
src = "hello world!"
runes2 := string(src)
fmt.Println("runes2",runes2)
desBytes := make([]byte, len(src))
copy(desBytes, src)
dst1 = string(desBytes)
fmt.Println(src, dst1)
str2 := src + str
fmt.Println(str2)
builder := strings.Builder{}
builder.WriteString(src)
builder.WriteString(str)
fmt.Println(builder)
fmt.Println(builder)
}