與go邂逅(二)——go當中的基本程序結構(數組 切片 map string)

前言

學習一門語言的時候,不免從最簡單的程序結構學起,這些東西在掌握了一門別的開發語言的狀況(如大名鼎鼎的java),就會顯得如魚得水了,下面會把我學習一些簡單例子分享出來。java

基本程序結構

快速爲一些變量賦值linux

const (
	NUM1 = 1 + iota
	NUM2
	NUM3
	NUM4
)

//輸出結果:1,2,4,8
func TestPrint(t *testing.T) {
	t.Log(NUM1, NUM2, NUM3, NUM4)
}
複製代碼

快速的實現一些數值交換數組

//數值交換
func TestExchange(t *testing.T) {
//也能夠這樣定義變量:var aa int = 1
	a := 1
	b := 2
	t.Log(a, b)
    //交換數值
	b, a = a, b
	t.Log(a, b)
}
複製代碼

類型轉換安全

//給類型命名
type typeInt int64

func TestInt(t *testing.T) {
	var a int64 = 2
	var b int32 = 3
	//類型不可轉
	//a = b
	var c = typeInt(3)

	t.Log(a, b, c)
}
複製代碼

實現斐波拉切數列的兩種方式學習

//斐波拉切
func TestFibList(t *testing.T) {
	var a int = 1
	var b int = 1
	t.Log(a)
	for i := 0; i < 5; i++ {
		t.Log(b)
		tmp := a + b
		a = b
		b = tmp
	}
}

//斐波拉切 遞歸
func TestFibRecursion(t *testing.T) {
	t.Log(FibRecursion(5))
}

func FibRecursion(i int) (result int) {
	if i == 1 || i == 2 {
		return 1
	}
	return FibRecursion(i-1) + FibRecursion(i-2)
}
複製代碼

數組比較,和java不一樣,不是比較指針,能夠比較值的ui

//數組比較
func TestCompareArray(t *testing.T) {
	a := [...]int{1, 2, 3, 4}
	b := [...]int{1, 2, 2, 4}
	c := [...]int{1, 2, 3, 4}
	t.Log(a == b) //false
	t.Log(a == c) //true
}
複製代碼

go也是有指針的,可是沒有細看,只是寫個例子看下結果spa

func TestPoint(t *testing.T) {
	var a int64 = 1
	var aPtr = &a
	t.Log(a, aPtr)// 1 0xc420018230
	//打印類: int64 *int64
	t.Logf("%T %T", a, aPtr)
}
複製代碼

string的默認值線程

func TestString(t *testing.T) {
	//默認值是"" 不是java的那種null
	var str string
	t.Log("+" + str + "+")//輸出++
}
複製代碼

for循環3d

//for循環 go當中原來沒有while
func TestFor(t *testing.T) {
	n := 5
	for n > 0 {
		t.Log(n)
		n--
	}
}

//for循環實現冒泡排序
func TestForSort(t *testing.T) {
	a := [...]int{3, 5, 2, 6, 4, 8, 2, 9,1,23,2,34,4,55,11}
	for i := 0; i < len(a)-1; i++ {
		for j := 0; j < len(a)-i-1; j++ {
			if a[j] > a[j+1] {
				tmp := a[j]
				a[j] = a[j+1]
				a[j+1] = tmp
			}
		}
	}

	t.Log(a)//[1 2 2 2 3 4 4 5 6 8 9 11 23 34 55]

}
複製代碼

go當中的條件判斷,寫起來仍是很爽的指針

//比較
func TestCondition(t *testing.T){
    //能夠條件結果賦值給變量
	if a:=3>2;a{
		t.Log("3>2")
	}

	// GOOS is the running program's operating system target:
	// one of darwin, freebsd, linux, and so on.
	switch runtime.GOOS{
	//自帶break
	case "darwin":
		t.Log("darwin")
	case "freebsd":
		t.Log("freebsd")
	case "linux":
		t.Log("linux")
	default:
		t.Log("default")
	}

	switch  {
	case 4>2:
		t.Log("4>2")
	case 4<2:
		t.Log("4<2")

	}
}
複製代碼

string的默認值

func TestString(t *testing.T) {
	//默認值是"" 不是java的那種null
	var str string
	t.Log("+" + str + "+")//輸出++
}
複製代碼

for循環

//for循環 go當中原來沒有while
func TestFor(t *testing.T) {
	n := 5
	for n > 0 {
		t.Log(n)
		n--
	}
}

//for循環實現冒泡排序
func TestForSort(t *testing.T) {
	a := [...]int{3, 5, 2, 6, 4, 8, 2, 9,1,23,2,34,4,55,11}
	for i := 0; i < len(a)-1; i++ {
		for j := 0; j < len(a)-i-1; j++ {
			if a[j] > a[j+1] {
				tmp := a[j]
				a[j] = a[j+1]
				a[j+1] = tmp
			}
		}
	}

	t.Log(a)//[1 2 2 2 3 4 4 5 6 8 9 11 23 34 55]

}
複製代碼

go當中的條件判斷,寫起來仍是很爽的

//比較
func TestCondition(t *testing.T){
    //能夠條件結果賦值給變量
	if a:=3>2;a{
		t.Log("3>2")
	}

	// GOOS is the running program's operating system target:
	// one of darwin, freebsd, linux, and so on.
	switch runtime.GOOS{
	//自帶break
	case "darwin":
		t.Log("darwin")
	case "freebsd":
		t.Log("freebsd")
	case "linux":
		t.Log("linux")
	default:
		t.Log("default")
	}

	switch  {
	case 4>2:
		t.Log("4>2")
	case 4<2:
		t.Log("4<2")

	}
}
複製代碼

string的基本操做

func TestString(t *testing.T) {
	//分割:[a b c]
	s := "a,b,c"
	splitStr := strings.Split(s, ",")
	t.Log(splitStr)

	//拼接:a:b:c
	stringJoin := strings.Join(splitStr, ":")
	t.Log(stringJoin)

	//整型轉字符串
	str := strconv.Itoa(10)
	t.Log(str)

	//字符串轉整型
	if i, e := strconv.Atoi("10"); e == nil {
		t.Log(i)
	}

複製代碼

map初始化

func TestInitMap(t *testing.T) {
	m1 := map[int]int{1: 2, 3: 4, 5: 6}
	t.Log(m1[2])
	t.Logf("len m1=%d", len(m1))

	m2 := map[int]int{}
	m2[4] = 16
	t.Logf("len m2=%d", len(m2))

	m3 := make(map[int]int, 10)
	t.Logf("len m3=%d", len(m3))
}
複製代碼

map判斷key是否存在和循環

func TestExistKey(t *testing.T) {
	m1 := map[int]int{}
	t.Log(m1[1])
	m1[2] = 0
	t.Log(m1[2])
	m1[3] = 0
	if v, ok := m1[3]; ok {
		t.Logf("Key 3's value is %d", v)
	} else {
		t.Log("key 3 is not existing.")
	}
}

func TestForMap(t *testing.T) {
	m1 := map[int]int{1: 1, 2: 4, 3: 9}
	for k, v := range m1 {
		t.Log(k, v)
	}
}
複製代碼

map當中的工廠模式

func TestFunc(t *testing.T) {
	m := map[int]func(op int) int{}
	m[1] = func(op int) int { return op }
	m[2] = func(op int) int { return op * op }
	m[3] = func(op int) int { return op * op * op }
	t.Log(m[1](2), m[2](2), m[3](2))
}
複製代碼

map模擬一個set

func TestMapForSet(t *testing.T) {
	mySet := map[int]bool{}
	mySet[1] = true
	n := 3
	//判斷是否存在
	if mySet[n] {
		t.Logf("%d is existing", n)
	} else {
		t.Logf("%d is not existing", n)
	}
	mySet[3] = true
	t.Log(len(mySet))
	delete(mySet, 1)
	n = 1
	if mySet[n] {
		t.Logf("%d is existing", n)
	} else {
		t.Logf("%d is not existing", n)
	}
}
複製代碼

線程安全map

//store:存儲一個設置的鍵值
//LoadOrStore:返回鍵的現有值(若是存在),不然存儲並返回給定的值,若是是讀取則返回true,若是是存儲返回false
//Load:讀取存儲在map中的值,若是沒有值,則返回nil。OK的結果表示是否在map中找到值。
func TestSyncMap(t *testing.T)  {

	m := sync.Map{}

	v, ok := m.LoadOrStore("1", "one")
	t.Log(v, ok) //one false

	v, ok = m.Load("2")
	t.Log(v, ok) //nil false

	v, ok = m.LoadOrStore("1", "oneone")
	t.Log(v, ok) //one true

	v, ok = m.Load("1")
	t.Log(v, ok) //one true

	m.Store("1", "oneone")
	v, ok = m.Load("1")
	t.Log(v, ok) // oneone true
	
}
複製代碼

更多文章可關注公衆號:stonezplxjj和我的博客:www.zplxjj.com

相關文章
相關標籤/搜索