zoukankan      html  css  js  c++  java
  • go类型系统

    https://studygolang.com/articles/18822?tdsourcetag=s_pcqq_aiomsg

    https://blog.csdn.net/hittata/article/details/50915496

    https://blog.csdn.net/hittata/article/details/51250179

    pre-declared type (预声明类型)

    golang 默认的有几个预声明类型:boole,num(所有的数字类型),string 这些预声明类型被用来构造其他的类型。

    composite types(复合类型)

    由其他预声明类型组合而成(没有使用type 关键字定义),如: array,struct,pointer,slice,map,channel,function,interface 
    注意:其中的struct,interface 特指匿名的类型,没有使用type包装

    命名类型和非命名类型
    named type : 一个类型通过一个标识符标示就是命类型名字(type name)

    unamed type: 一个类型是由先前已声明类型组合构成被称为类型字面量(type literal)

    二者区别:
    named type 可以有自己的methods, mehtods 是一种特殊形式的函数,该函数的第一个参数是该type的变量或指向该类型变量的指针(receiver).

    unnamed type:不能定义自己的method

    type 包括 特点
    named type pre-declared
    type declared new type
    可以定义自己的method
    unnamed type type literal 不可以定义自己的method

    一个特别重要的事情需要记住:pre-declared types 也是命名类型。所以:int 是named type , 但是 *int ,[]int 不是。

    特别注意:我们不能直接为int 定义method

    func (a int) methodname() {

    }

    不是因为int 是unnamed type ,而是为一个type定义方法必须在该类型的所在的package , int 的scope (作用域是)universe (全局的),int 是语言层面预声明的,其属于任何package,也就没有办法为其增加method.

    package main
     
    import (
            "fmt"
    )
     
    func (a int) Print() {
            fmt.Println(a)
    }
     
    func main() {
              m := 12
              a.Print()
    }
    [root@localhost /project/go/src/test]#go build name.go 
    # command-line-arguments
    ./name.go:7: cannot define new methods on non-local type int
    ./name.go:13: undefined: a

    Underlying Type 底层类型
    所有type(类型)都有个underlying type(底层类型)。Pre-declared types 和type literals 的底层类型是它们自身;通过type newtype oldtype 的底层类型,是逐层递归向下查找的,直到查到的oldtype 是Pre-declared types 和type literals。

    type underlying type 示例
    pre-declared type itself string 的underlying type 还是string
    type literal itself map[string]string的underlying type 还是map[string]string

    通过type定义的新类型:
    type newtype oldtype

    逐层向下递归查找oldtype,直到oldtype是pre-declared type 或type literal;找到的pre-declared type 或type literal就是underlying type

    type Map map[string]string
    type SpecialMap Map
    则:Map 和 SpecialMap 的underlying type
    都是map[string]string

    另一条重要规则是:
    type newtype oldtype: newtype不会从oldtype或其underlying type继承任何methods .
    有两个特例就是:接口和组合类型(composite type)

    new type Methods SET
    type interface2 interface1 interface2 会继承interface1的methods set

    type NewType struct {
      InnerType
    }

    NewType 会继承InnerType的methods set
    其他type newtype oldtype newtype 不会从oldtype 或其underlying type中继承任何方法

    这里表达的意思就是:一旦你定义一个新类型,你往往是想为其定义一个新的methods set 

     static type 静态类型和dynamic type 动态类型

    static type就是变量定义声明时的类型,
    只有interface 有动态类型
    接口类型变量有dynamic type,接口的动态类型是其在运行时绑定值的类型
    接口的动态类型可以在运行时发生变化,但是动态类型其必须满足可以赋值给接口类型的条件,否则会发生panic

    非interface 的类型的dynamic type 就是其static type

  • 相关阅读:
    SQL查询
    SQL总结(二)连表查询
    SQL总结(一)基本查询
    SQL游标、函数的使用方法
    理解 Virtual DOM(摘)及评价
    virtual DOM的作用:将DOM的维护工作由系统维护转交给virtual DOM维护
    DOM的回流和重绘(重排、重绘)
    并发编程的核心
    执行流(指令流)的控制层级
    go语言的工资好高
  • 原文地址:https://www.cnblogs.com/answercard/p/9017770.html
Copyright © 2011-2022 走看看