go 1.0.3 入门

package main

import "fmt"
import "os"

var (
	a int
	b int
)

func testArrayParmter(arr [2]int) {
	//arr传进来是个副本,所以函数内改变arr,对外部不影响
	println("")
	for i := range arr {
		arr[i] += 1
		print(arr[i])
		if !(i+1 == len(arr)) {
			print(",")
		}
	}
	println("")
}
func testSliceParmter(arr []int) {
	println("")
	for i := range arr {
		arr[i] += 1
		print(arr[i])
		if !(i+1 == len(arr)) {
			print("^,")
		}
	}
	println("")
}
func main() {
	for i := 0; i < 5; i++ {
		for j := 0; j <= i; j++ {
			print("A")
		}
		println("")
	}
	a = 0x77
	b = 077
	const c = "i am const var"
	const (
		d = iota
		e
		f = iota
	)
	s := "hello" + " world"
	ch := []byte(s) //将s转成字节数组
	ch[0] = 'c'
	s2 := string(`
		ssfswerq
	`)

	if 1 < 2 {
		if 1 > 0 && !false {
			fmt.Printf("0<1<2\n")
		} else {
			fmt.Printf("not 0<1<2\n")
		}
	}
	var cc complex128 = 5 + 5i

	fmt.Printf("d=%d e=%d f=%d s2=%s cc=%v\n", d, e, f, s2, cc)
	fmt.Printf("s=%s a=%d b=%d c=%s\n", s, a, b, c)
	file, err := os.OpenFile("c:\\2.py", os.O_RDONLY, 0)
	if err != nil {
		fmt.Println(err)
		return
	}
	by := []byte(s)
	file.Read(by)
	fmt.Println(string(by)) //读取一行
	dd, err := file.Stat()
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(dd)
	file.Close()

	//3种for的使用
	for i := 1; i < 2; i += 1 {
		fmt.Println(i)
	}
	i := 2
	flag := true
Here: //goto的使用
	for i >= 0 {
		fmt.Println(i)
		i -= 1
	}
	for {
		println("while True{}")
		break
	}

	if flag {
		flag = false
		goto Here
	}
	j := 0
J:
	for i = 0; i < 100; i++ {
		for j = 0; j < 100; j++ {
			if i > 5 {
				break J //跳出J标签所在的循环层
			}
		}
	}
	println(i, j)
	println("====range的使用====")
	list := []string{"a", "b", "c"}
	for k, v := range list {
		println(k, v)
	}
	for pos, cha := range "abc" {
		fmt.Printf("#pos=%d cha='%c'\n", pos, cha) //为何这个会跑到while True{}前面输出
	}
	println("switch的使用")
	ccc := 'A'
	switch { //如果switch没有表达式,它会匹配true
	//转化为一个个匹配,知道为true
	case '0' <= ccc && ccc <= '9':
		println('0')
	case 'a'-32 <= ccc && ccc <= 'f'-32:
		println('a')
	case 'A' <= ccc && ccc <= 'F': //不会执行
		println('A')
	}
	switch j {
	case 1:
		println("case 1")
	case 2:
		println("case 2")
	default:
		println("case default")
	}
	ch = []byte("hhhhhh")
	ch2 := []byte("hhhhhhh")
	println(len(ch), len(ch2))
	for pos := range ch { //比较字符串大小
		switch {
		case ch[pos] > ch2[pos]:
			println("big")
		case ch[pos] < ch2[pos]:
			println("small")
		}
	}
	println("数组的使用")
	arr := [2]int{1, 2} //这两个数组都初始化为1,2
	var arr2 [2]int
	arr2 = arr //数组同样是值类型的:将一个数组赋值给另一个数组,会复制所有的元素
	for i := range arr2 {
		print(arr2[i])
	}
	testArrayParmter(arr)
	for i := range arr2 {
		print(arr[i])
	}
	println("")
	arr3 := [...]int{1, 2, 3, 4, 5, 6, 7, 8}
	println(len(arr3))
	println("数组的创建")
	a3 := [3][2]int{[2]int{1, 2}, [...]int{1, 2}, {3, 4}}
	println(len(a3))
	println("slice的使用---slice是指向数组头部的指针,且是引用类型,要用make创建")
	sl := make([]int, 10) //创建了一个保存有10个元素的slice
	sl[1] = 2
	println(sl[1])
	sl2 := arr3[2:4]
	println(sl2[0], sl2[1], len(sl2), arr3[:len(arr3)])
	println("使用内置函数append和copy")
	sa := []int{0, 0}
	sb := append(sa, 2, 2)
	sc := append(sb, sa...)        //{0,0,2,2,0,0}
	for i := 0; i < len(sc); i++ { //会跑到goto那去
		fmt.Printf("=%d=", sc[i])
	}
	println("")
	var a4 = [8]int{0, 1, 2, 3, 4, 5, 6, 7}
	var ss = make([]int, 6)
	//copy最多复制的元素是两个参数数组的最小长度
	n1 := copy(ss, a4[0:]) //因为s的长度为6,故此时s=={0,1,2,3,4,5}
	n2 := copy(ss, ss[2:]) //由于复制重叠了,故此时s=={2,3,4,5,4,5}
	println(n1, n2)
	testSliceParmter(ss) //返回后ss=={3,4,5,6,5,6}
	println("map的使用")
	monthdays := map[string]int{
		"Jan": 31, "Feb": 28, "Mar": 31,
		"Apr": 30, "May": 31, "Jun": 30,
		"Jul": 31, "Aug": 31, "Sep": 30,
		"Oct": 31, "Nov": 30, "Dec": 31, //注意最后一个逗号不能少
	}
	println(monthdays["Nov"])
	year := 0
	for _, days := range monthdays {
		year += days
	}
	monthdays["ckl_day"] = 31 //向map添加新项

	val, ok := monthdays["ckl_day"] //判断monthdays["ckl_day"]是否存在,保存在ok中
	//删除monthdays["ckl_day"]项
	delete(monthdays, "ckl_day")
	year2 := 0
	for _, days := range monthdays {
		year2 += days
	}
	val1, ok1 := monthdays["ckl_day"]
	println(year, monthdays["ckl_day"], val, ok, year2, val1, ok1)

}



posted @ 2013-05-18 20:04  javawebsoa  Views(209)  Comments(0Edit  收藏  举报