go 语法入门

go 语法

循环

package main

import "fmt"

func main() {
	i := 0
	for i < 1000 {
		i++
		fmt.Println("循环次数:", i)
	}

	/* 定义局部变量 */
	var fora int = 0

	/* 循环 */
LOOP:
	if fora < 20 {
		/* 跳过迭代 */
		fora++
		fmt.Printf("a的值为 : %d\n", fora)
		goto LOOP
	}

}

函数,返回两个参数

package main

import "fmt"

func functest(x, y string) (string, string) {
	return x, y
}

func main() {
	a, b := functest("a", "b")
	fmt.Println(a, b)
}

数组遍历, 切片

package main

import "fmt"

func main() {
	nums := []int{0, 1, 2, 3, 4, 5, 6}

	numsLen := len(nums)
	i := 0
	for i < numsLen {

		fmt.Println(nums[i])
		fmt.Println(nums[0 : i+1])
		i++
	}
}

map

import "fmt"

func main() {
	var mapTests map[string]string
	mapTests = make(map[string]string)

	mapTests["a"] = "1"
	mapTests["b"] = "2"
	mapTests["c"] = "3"
	mapTests["d"] = "4"

	// 遍历
	for item := range mapTests {
		fmt.Println(item, ":", mapTests[item])
	}

	/*查看元素在集合中是否存在 */
	capital, ok := mapTests["a"]
	if ok {
		fmt.Println("a =", capital)
	} else {
		fmt.Println("a no found")
	}

	delete(mapTests, "a")
	fmt.Println("delete a")

	capital, ok = mapTests["a"]
	if ok {
		fmt.Println("a =", capital)
	} else {
		fmt.Println("a no found")
	}
}

你可能感兴趣的:(Go,学习,go,语法)