go语言的基础语法

字符串数组

package main

import (
	"fmt"
)

func main() {
	var str string
	str = "hello world"
	ch := str[0]
	fmt.Printf("the length of\"%s\" is %d \n", str, len(str))
	fmt.Println("the first character of\"%s\"is %c.\n", str, ch)
}

基本变量

package main

import (
	"fmt"
)

func main() {
	/*变量使用
	var v1, v2, v3 int
	var v4, v5 bool
	v1, v2, v3 = 10, 20, 30
	v4, v5 = true, false
	fmt.Println(v1, v2, v3, v4, v5)
	fmt.Println("Hello World!")
	v4, v5 = v5, v4
	v1, v2, v3 = v3, v1, v2//赋值变量交换,不限数量
	fmt.Println(v1, v2, v3, v4, v5)
	v6 := 100//省略var声明的定义赋值 :=
	fmt.Println(v6)
	*/
	/*常量使用
	const N = 100
	const MAX_SIZE = 10
	fmt.Println(N, MAX_SIZE)
	const School = "China West Normal University"
	fmt.Println(School)
	*/
	/*字节切片转换字符串
	s := "hello"
	c := []byte(s) // 将字符串 s 转换为 []byte 类型
	c[0] = 'c'
	c[3] = 'c'
	s2 := string(c) // 再转换回 string 类型
	fmt.Printf("%s\n", s2)
	*/
	/*指针数组
	a, b, c := 10, 20, 30
	var arr [3]*int//定义的是数组存地址
	arr[0] = &a
	arr[1] = &b
	arr[2] = &c
	for i := 0; i < 3; i++ {
		fmt.Printf("%5d", *arr[i])//引用数组地址指向的值
	}
	*/
	/*数组作为函数参数
	func main() {
		x := [5]int{1, 2, 3, 4, 5}
		f(x, true)
		f(x, false)
	}
	func f(a [5]int, b bool) {
		for i := 0; i < len(a); i++ {
			if b {
				a[i] += 10
			}
			fmt.Printf("%5d", a[i])
		}
		fmt.Println()
	}
	*/
	/*map定义并赋初值
	dict := map[string]int{"abc": 12, "bcd": 100, "chen": 200}
	//var 字典名 map[类型1]类型2   ,其中类型1为索引类型,类型2为存放的数据类型。
	for x, y := range dict {
		fmt.Println(x, y)
	}
	*/

}

数组

package main

import (
	"fmt"
)

func main() {

	array := [5]int{1, 2, 3, 4, 5}
	fmt.Println("Hello World!")
	for i := 0; i < len(array); i++ {
		fmt.Println("element", i, "of array is", array[i])
	}
}

多变量赋值

package main

import (
	"fmt"
)

func main() {
	fmt.Println("Hello World!")
	var i int32
	var j int64

	i, j = 1, 2
	/*if i == j {
		fmt.Println("i and j are equal.")

	}*/
	if i == 1 || j == 2 {
		fmt.Println("i and j are equal.")
	}
}
posted @ 2020-12-24 21:41  东血  阅读(142)  评论(0编辑  收藏  举报

载入天数...载入时分秒...