Go学习笔记(二)

  1. 定义结构体:

        type struct_variable_type struct {
       member definition;
       member definition;
       ...
       member definition;
    }
  2. 结构体example:

    // struct_test2.go
    
    package main
    
    import (
        "fmt"
    )
    
    type Book struct {
        author  string
        title   string
        book_id int
        subject string
    }
    
    func main() {
        var book1 Book
        book1.author = "lryong1"
        book1.title = "test1"
        book1.subject = "wulala"
        book1.book_id = 1
    
        var book2 Book
        book2.author = "lryong2"
        book2.title = "test2"
        book2.subject = "wakaka"
        book2.book_id = 2
    
        gen_inform(&book1) //传递book1结构体的地址
        gen_inform(&book2)
    
    }
    
    func gen_inform(*a Book) { //结构体指针
        fmt.Printf("Book's title is %s\n", a.title)
        fmt.Printf("Book's author is %s\n", a.author)
        fmt.Printf("Book's book_id is %d\n", a.book_id)
        fmt.Printf("Book's subject is %s\n", a.subject)
        fmt.Printf("Book's information is %d\n", a.try_method())
        fmt.Println("============================")
    }
    
    func (a Book) try_method() int {
        return a.book_id << 4
    }
  3. Golang的双引号和反引号,单引号:

    • 双引号和反引号都用于表示一个常量字符串。
    • 双引号用来创建可解析的字符串字面量(支持转义,但不能用来引用多行)。
    • 反引号用来创建原生的字符串字面量,这些字符串可能由多行组成(不支持任何转义序列),原生的字符串字面量多用于书写多行消息、HTML以及正则表达式。
    • 而单引号则用于表示Golang的一个特殊类型:rune,类似其他语言的byte但又不完全一样,是指:码点字面量(Unicode code point),不做任何转义的原始内容。
  4. Go 数组的长度不可改变,在特定场景中这样的集合就不太适用,Go中提供了一种灵活,功能强悍的内置类型切片(“动态数组”),与数组相比切片的长度是不固定的,可以追加元素,在追加时可能使切片的容量增大。
  5. Go 语言中 range 关键字用于for循环中迭代数组(array)、切片(slice)、通道(channel)或集合(map)的元素。在数组和切片中它返回元素的索引值,在集合中返回 key-value 对的 key 值。example:

    // range_test1.go
    package main
    
    import (
        "fmt"
    )
    
    func main() {
        //使用range求一个slice的和,跟使用数组相似
        var test = []int{2, 4, 6}
        sum := 0
    
        for _, num := range test { //"_"只是可写变量,省略索引
            sum += num
        }
    
        fmt.Println("sum result is ", sum)
    
        for i, v := range test {
            if v == 6 {
                fmt.Println("index:", i)
            }
        }
        //range也可以用在map的键值对上。
        kvs := map[string]string{"a": "apple", "b": `banana`}
        for k, v := range kvs {
            fmt.Printf("%s -> %s\n", k, v)
        }
        //range也可以用来枚举Unicode字符串。第一个参数是字符的索引,第二个是字符(Unicode的值)本身
        for i, c := range "go" {
            fmt.Printf("%d -> %d\n", i, c)
        }
    
    }
  6. Go语言Map(集合):Map 是一种集合,所以我们可以像迭代数组和切片那样迭代它。不过,Map 是无序的,我们无法决定它的返回顺序,这是因为 Map 是使用 hash 表来实现的。定义方式:

        /* 声明变量,默认 map 是 nil */
    var map_variable map[key_data_type]value_data_type
    
    /* 使用 make 函数 */
    map_variable = make(map[key_data_type]value_data_type)
    • example:
          // map_test1.go
      package main
      
      import (
          "fmt"
      )
      
      func main() {
          // var countryCapitalMap map[string]string //gen map,无序
          countryCapitalMap := make(map[string]string)
      
          countryCapitalMap["France"] = "Paris"
          countryCapitalMap["Italy"] = "Rome"
          countryCapitalMap["Japan"] = "Tokyo"
          countryCapitalMap["India"] = "New Delhi"
          countryCapitalMap["United States"] = "New York"
      
          for country := range countryCapitalMap {
              fmt.Printf("The capital of %s is %s\n", country, countryCapitalMap[country])
          }
      
          capital, ok := countryCapitalMap["United States"] //查看元素在集合中是否存在
          if ok {
              fmt.Println("Capital of United States is ", capital, "!!!")
          } else {
              fmt.Println("Capital of United States is not present.")
          }
      
          delete(countryCapitalMap, "United States")
          fmt.Println("United States has been deleted.")
      }
  7. 接口:Go 语言提供了另外一种数据类型即接口,它把所有的具有共性的方法定义在一起,任何其他类型只要实现了这些方法就是实现了这个接口。

    • example:
      // interface_example1.go
      package main
      
      import (
          "fmt"
      )
      
      type Phone interface {
          call()
      }
      
      type NokiaPhone struct{}
      
      func (phone NokiaPhone) call() {
          fmt.Println("I'm Nokia,I can call you!")
      }
      
      type IPhone struct{}
      
      func (phone IPhone) call() {
          fmt.Println("I'm iphone,I can call you!")
      }
      
      func main() {
          var test_phone Phone
          test_phone = new(NokiaPhone)
          test_phone.call()
      
          test_phone = new(IPhone)
          test_phone.call()
      }
  8. Go 语言通过内置的错误接口提供了非常简单的错误处理机制。error类型是一个接口类型.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值