与go邂逅(二)——go当中的基本程序结构(数组 切片 map string)

前言

学习一门语言的时候,难免从最简单的程序结构学起,这些东西在掌握了一门别的开发语言的情况(如大名鼎鼎的java),就会显得如鱼得水了,下面会把我学习一些简单例子分享出来。

基本程序结构

快速为一些变量赋值

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不同,不是比较指针,可以比较值的

//数组比较
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也是有指针的,但是没有细看,只是写个例子看下结果

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循环

//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和个人博客:http://www.zplxjj.com
《与go邂逅(二)——go当中的基本程序结构(数组 切片 map string)》

    原文作者:stone想静静
    原文地址: https://segmentfault.com/a/1190000019000960
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞