type-system-in-go
0.1 · Type System In Go#
0.1.1 · 1. Type System#
01构成,数据无意义;人为定义数据,具有意义;类型对数据添加属性,可对数据进行分类(数据的表示,类型检查),对不同的数据类型如何操作(数据的行为),不同的数据类型之间如何互相作用。
类型系统是什么? 类型系统的好处?
类型系统的作用是定义编程语言中值和表达式的类型,将它们归类,赋予它们不同的行为,指导它们如何相互作用。
0.1.2 · 2. Go Type System Overview#
If you don’t understand the data, you don’t understand the problem. All problems are unique and specific to the data you are working with. Data transformations are at the heart of solving problems. Each function, method and work-flow must focus on implementing the specific data transformations required to solve the problems.
0.1.2.1 · 2.1. Basic Types vs. Composite Types#
0.1.2.1.1 · 2.1.1. Basic Types#
boolean type(bool),
numeric(int8, uint8(byte), int16, uint16, int32(rune), uint32, int64, uint64, int, uint, uintptr,float32, float64, complex64, complex128),
string(string)
0.1.2.1.2 · 2.1.2. Composite types#
复杂数据的表示, be constructed using type literals:
array, slice(len, cap, underlying array), map(hashmap):
struct(a sequence of fields):
pointer(pointers to variables of a given type):
function(same parameter and result types):
interface(a type set):
channel(CSP):
0.1.2.2 · 2.2. Generic#
使用的时候再决定具体类型
type parameter
function, type works
Instantiations: substitute, check if implements constraint
0.1.2.3 · 2.2. Syntax: Type Definitions & Type Alias Declarations#
类型定义 -> 产生新类型 类型别名 -> 未产生新类型
0.1.2.4 · 2.3. Named Types vs. Unnamed Types#
0.1.2.4.1 · 2.3.1. Named Types#
Predeclared types, defined types, and type parameters are called named types.
An alias denotes a named type if the type given in the alias declaration is a named type.
0.1.2.4.2 · 2.3.2. Unnamed Types#
Other types are called unnamed types.
0.1.2.5 · 2.4. Underlying Types & Core types & Type Identity & Assignability#
Each type T has an underlying type: If T is one of the predeclared boolean, numeric, or string types, or a type literal, the corresponding underlying type is T itself. Otherwise, T’s underlying type is the underlying type of the type to which T refers in its declaration. For a type parameter that is the underlying type of its type constraint, which is always an interface.
non-interface type: same as the underlying type. interface type: one of the following conditions is satisfied:
- a single type U which is the underlying type of all types in the type set of T(core type is type U);
- the type set of T contains only channel types with identical element type E, and all directional channels have the same direction(core type is type chan E or chan<- E or <-chan E)
A named type is always different from any other type. Otherwise, two types are identical if their underlying type literals are structurally equivalent;
0.1.2.6 · 2.5. Pointers vs. Values#
Pointers: indirect Values: direct
0.1.2.7 · 2.6. Reflections & Interfaces#
0.1.2.8 · 3. Dynamic Typing vs. Static Typing#
[]: https://lenciel.com/2016/09/types-in-programming-languages “types-in-programming-languages” []: https://rakyll.org/typesystem/ “The Go type system for newcomers” []: https://www.jianshu.com/p/e1991e6c3fff “Golang的Named Type与Unamed Type” []: https://en.wikipedia.org/wiki/Data-oriented_design “Data-Oriented Design” []: https://stackoverflow.com/questions/1641580/what-is-data-oriented-design “what is data oriented design”