Go的语法基础|青训营

67 阅读2分钟

青训营的第一节课带我们了解的是Go语言的语法基础,相信学习过其它编译语言的同学们都知道,各语言的语法基础都大同小异。学习后我们能发现Go语言的语法较为简单。接下来就带大家发现Go语言的魅力。 Go语言具有以下特性: 1、高性能、高并发 2、语法简单、学习曲线平缓 3、丰富的标准库 4、完善的工具链 5、静态链接 6、快速编译 7、跨平台 8、垃圾回收 接下来为大家一一讲解Go语言的语法 1、变量 例子: package main

import ( " fmt " "math " )

func main(){ var a = "initial "

 var b, c int =12 

 var e  float64 

f := float 32 (e)

g :=a +"foo "

fmt.Println(a,b,c,d,e,f)∥ initial 1 2 true 0 0 fmt.Println(g) // initialapple

const s string = "constant " const h = 500000000 const i=3e20 /h fmt.Println(s,h,i,math,Sin(h),math.Sin(i))

}1

2、if else 例子: package main

import " fmt "

func main() {

    if  7%2==0{
        fmt.Println("7 is even ")
   }else{
       fmt.Println("7 is odd ")
   }

 if 8%4==0{
        fmt.Println("8 is divisible by 4 ")

if num :=9;num <0{ fmt.Println(num,"is negative ") }else if num <10{ fmt.Println(num,"has 1 digit ") }else{ fmt.Println(num,"has multiple digits) } } 这里是一些课程的链接: 课程源码:github.com/wangkechun/… 短链接:hi-hi.cn/go

3.循环 例子: `package main import "fmt"

func main(){ i:=1 for{ fmt.Println("loop") break } for j:=7;j<9;j++{ fmt.Println(j) } for n:=0;n<5;n++{ if n%2==0{ continue } fmt.Println(n) } for i <=3{ fmt.Println(i) i=i+1 } }` 4.swith


import (
 "fmt"
 "time"
 )
 
 func main() {
 
      a :=2
      switch a{
      case 1:
          fmt.Println("one")
      case 2:
          fmt.Println("two")
      case 3:
          fmt.Println("three")
      case 4,5:
          fmt.Println("four or five")
      default:
          fmt.Println("other")
          }
          
          t :=time.Now()
          swith{
          case t.Hour()<12:
          fmt.Println("It's before noon")
          defualt
          fmt.Println("It's after noon")
          }
          }

5.数组 例子:


   import "fmt"

  func main() {
  
     var a [5]int
     a[4] = 100
     fmt.Println(a[4],len(a))
     
     b :=[5]int{1,2,3,4,5}
     fmt.Println(b)
     
     var twoD [2][3]int
     for i :=0;j<3;j++{
        twoD[i][j]=i+j
        }
        }
        fmt.Println("2d:",twoD)
        }

6.切片 例子:


  import "fmt"
  
  func main(){
  
     s :=make([]string,3)
     s[0] = "a"
     s[1] = "b"
     s[2] = "c"
     fmt.Println("get:",s[2])  //c
     fmt.Println("len:",len(s))  //3
     
     s = append(s,"d")
     s = append(s,"e","f")
     fmt.Println(s)  //[a b c d e f]
     
     c :=make([]string,len(s))
     copy(c,s)
     fmt.Println(c) //[a n c d e f]
     
     fmt.Println(s[2:5])  //[c d e]
     fmt.Println(s[:5])   //[a b c d e]
     fmt.Println(s[2:])   //[c d e f]
     
     good :=[]string{"g","o","o","d"}
     fmt.Println(good)  //[g o o d]
     ```
     
  7.map
  例子:
  package main
  
  import "fmt"
  
  func main() {
    m :=make(map[string]int)
    m["one"] = 1
    m["two"] = 2
    fmt.Println(m)        //map[one:1 two:2]
    fmt.Println(len(m))   //2
    fmt.Println("one")  //1
    fmt.Println(m[unknow]) //0
    
    r,ok :=m["unknow"]
    fmt.Println(r,ok) // 0 flase
    
    delete(m,"one")
    
    m2 :=map[string]int{"one":1,"two":2}
    var m3=map[string]int{"one":1,"two":2}
    fmt.Println(m2,m3)
    
    今天就先到此为止了,总的来说go的语法看起来较为简洁,且它有自己独特的特点,希望能把基础语法弄懂弄透,为之后的学习打下夯实的基础。