• Let's GO(一)


    近来开始学Go,留此博客以记录学习过程,顺便鞭策自己更加努力。

    人生苦短,Let's GO!

    简单介绍

    The Go Programming Language

    Go(又称Golang)是Google开发的一种静态强类型、编译型、并发型,并具有垃圾回收功能的编程语言。

    我学习主要参考七米老师的博客李文周的博客以及他在B站的视频,在此也感谢一下大佬无私分享。

    今天我学了什么有趣的东西?

    1.万物起源HelloWorld

    package main   //一个Go项目必须有一个main包
    
    import "fmt"   
    
    func main() {
    	//万物起源Hello World
    	fmt.Println("Hello World!")
    }
    

    2.iota

    const (
    	a1 = iota              //a = iota == 0
     	b1                         //b = iota == 1
    	c1                         //c = iota == 2
    )
    

    3.string

    //保留分行
    s3 := ` Hello 
    World`
    fmt.Println(s3)
    /* 
    Hello
    World
    */
    

    4.array

    //初始化按下标定义
    	str := [...]string{1:"java",3:"go",7:"c"}
    	fmt.Println(str)
    //[java go   c]
    

    哦差点忘了,数组不可变!

    5.slice

    解决数组不可变
    slice是引用数组的某一部分,改变slice会改变对应数组

    s3 := make([]int,5,10)
    fmt.Printf("%v,len:%v,cap:%v",s3,len(s3),cap(s3))
    //slice能跟nil(空)比较,但不能跟其他slice比较
    	if s == nil {//suggest len(s) == 0
    		fmt.Println("s is nil")
    	}
    	//! not nil after init,although it is empty
    	var s4 = []int{} //or s4 := make([]int,0) the same
    	if s4 == nil {
    		fmt.Println("s4 is nil") //you won't see it
    	}
    
    for i:=0; i<10; i++ {
    		s = append(s, i)    //向s末尾添加值i的元素,容量不足会自动扩大
    		fmt.Printf("%v,len:%v,cap:%v,ptr:%p
    ",s,len(s),cap(s),s)
    	}
    	//append s1(另一个切片)
    	s = append(s,s1...)
    	fmt.Println(s)
    
    //delete index: append(s[0:index],s[index+1:]...)
    	s8 := append(s[0:2],s[3:]...) //delete s[2]
    	fmt.Println(s8)
    
    //sort array
    	var b = [...]int{8,23,12,4,5}
    	sort.Ints(b[:])
    	fmt.Println(b)
    

    6.if for switch

    go 没有while,或者说,for expr {}就是while

    //if
    if i:=0; i>1 { //选择性定义
    		fmt.Println(i)
    	} else if i>2 {
    		fmt.Println(i)
    	} else {
    		fmt.Println(i)
    	}
    
    //switch
    switch str:="hello";str {  //不仅支持整形
    	case "he"+"llo":    //case可以使用表达式
    		fmt.Println("true")
    		fallthrough            //go每个case自动break,使用fallthrough执行下一case
    	default:
    		fmt.Println("false")
    	}
    
    //for
    //1.
    	for i:=0; i<10; i++ {
    		fmt.Println(i)
    	}
    //2.while
    	i := 10
    	for i>0 {
    		fmt.Println(i)
    		i--
    	}
    //for range
    var name = []int {1, 2, 3, 4, 5}
    	for j,k := range name {
    		fmt.Println(j,k)
    	}
    

    7.指针

    学过C的同学可能看到指针就会有点头皮发麻吗哈哈,go的指针很简便了

    a := 10
    pa := &a
    fmt.Printf("pa:%v,addr:%p,type:%T
    ",*pa,pa,pa)
    

    就这么简单的用法,*和&,没有偏移

    //还有感觉不太会用到的new
    b := new(int) //*b = 0
    	fmt.Println(*b)
    

    总而言之

    Go还是挺有趣的,语法中充满了一些对语法老前辈的不满哈哈。
    那么今天就学到这了,人生苦短,Let's GO!

  • 相关阅读:
    67. Add Binary
    66. Plus One
    64. Minimum Path Sum
    63. Unique Paths II
    How to skip all the wizard pages and go directly to the installation process?
    Inno Setup打包之先卸载再安装
    How to change the header background color of a QTableView
    Openstack object list 一次最多有一万个 object
    Openstack 的 Log 在 /var/log/syslog 里 【Ubuntu】
    Git 分支
  • 原文地址:https://www.cnblogs.com/syisyuan/p/12873048.html
Copyright © 2020-2023  润新知