Type is an important and commonly used keyword in the go grammar, and type is by no means just a typedef in C + +. Knowing the use of type will make it easy to understand the use of the core concepts struct, interface, and functions in go. I'll summarize the description with the example code below, and pay special attention to the comments in the code.
1. Define the structure body
Definition of the structure body
Type person struct {
Name string//Note the following cannot have commas
Age int
}
Func Main () {
The structure body is initialized
P: = person{
Name: "Taozs",//Note that you want to add a comma later
Age:18,//or the following} mentioned here to omit the comma
}
Fmt. Println (P.name)
}
Initialization fields are not necessarily all specified, for example, the following is OK, name defaults to an empty string of length 0
P: = person{
Age:18,
}
2, type equivalence definition, equivalent to type renaming
Type name string
The name type is equivalent to string
Example:
Type name string
Func Main () {
var myname name = "Taozs"/is actually a string type
L: = []byte (myname)//String byte array
Fmt. Println (Len (L))//byte length
}
However, be aware that type is by no means just a series of aliases defined. You can also define methods for new types.
The name type above can define a method like the following:
Type name string
Func (n name) len () int {
Return Len (n)
}
Func Main () {
var myname name = "Taozs"/is actually a string type
L: = []byte (myname)//String byte array
Fmt. Println (Len (L))//byte length
Fmt. Println (Myname.len ())//method of calling Object
}
3, the structure of anonymous members embedded in the body
Nested anonymous member definition in structure body
Type person struct {
string//Direct write type, anonymous
Age int
}
Func Main () {
Structure Anonymous member initialization
P: = person{string: "taozs", age:18}//can omit some fields, such as: person{string: "taozs"}. You can also omit the field name: person{"taozs", 18}, but you must write it all, you cannot omit part of the field
Structure Anonymous member access
Fmt. PRINTLN (p.string)//note cannot be cast with coercion type (type assertion): P. (string)
}
Here is an example of a single anonymous member.
Nested anonymous member definition in structure body
Type person struct {
String
}
Func Main () {
Structure Anonymous member initialization
P: = person{string: "taozs"}//can also be: person{"taozs"}
Structure Anonymous member access
Fmt. PRINTLN (p.string)//note cannot be cast with coercion type (type assertion): P. (string)
}
4. Define interface type
Package Main
Import (
"FMT"
)
Interface definition
Type Personer Interface {
Run ()
Name () string
}
Implement the interface, note that the implementation of the interface is not only a struct, but also can be a function object, see 5th below
Type person struct {
Name string
Age int
}
Func (person) Run () {
Fmt. Println ("Running ...")
}
The receive parameter person cannot be a pointer type, otherwise it is not considered to implement an interface
Func (P person) Name () string {
Return P.name
}
Func Main () {
Variable definition of interface type
var p personer
Fmt. PRINTLN (P)//value <nil>
Instantiate the struct body and assign it to interface
p = person{"taozs", 18}//or: &person{"taozs", 18}
P.run ()
Fmt. Println (P.name ())
var p2 person = p. (person)/type assertion, interface type assertion to specific type
Fmt. Println (P2.age)
}
In addition, a type assertion return value can have a second bool value indicating whether the assertion was successful, as follows:
If p2, OK: = P. (person); OK {//Assertion success OK value is true
Fmt. Println (OK)
Fmt. Println (P2.age)
}
5. Define function type
The following is the definition of a function type handler
Type handler func (name string) int
You can redefine the method for this function type, such as:
Func (H handler) Add (name string) int {
Return h (name) + 10
}
Let's take a look at the example, which deals with functions, function methods, structural methods, and the use of interfaces.
Package Main
Import (
"FMT"
)
Defining interfaces
Type Adder Interface {
Add (String) int
}
Defining function types
Type handler func (name string) int
Implementing function Type Methods
Func (H handler) Add (name string) int {
Return h (name) + 10
}
function parameter type accepts an object (function or struct) that implements the Adder interface
Func process (a adder) {
Fmt. Println ("Process:", A.add ("taozs"))
}
Another function definition
Func doubler (name string) int {
Return len (name) * 2
}
Non-function type
Type Myint int
Implements the Adder interface
Func (I myint) Add (name string) int {
Return len (name) + int (i)
}
Func Main () {
Note to be a function object you must explicitly define the handler type
var my handler = func (name string) int {
Return Len (name)
}
The following is a call to a function or function method
Fmt. Println (My ("taozs"))//Calling function
Fmt. Println (My.add ("taozs"))//method of calling function object
Fmt. Println (Handler (doubler). Add ("taozs"))//doubler function explicitly converts to a handler function object and then calls the Add method of the object
The following are calls for interface adder
The process (my)//process function requires Adder interface type parameters
Process (Handler (doubler))//Because the parameter types accepted by the process are handler, so here's a strong conversion
Process (Myint (8))//Implementation Adder interface can be not only a function, but also a structural body
}