Go language type Conversion
Type conversions are used to convert a variable of one data type to a variable of another type, and the basic format for the Go language type conversion is as follows:
type_name(expression)
Type_name is a type, and expression is a formula.
Instance
The following example converts a shape to a floating-point type, evaluates the result, and assigns the result to a floating-point variable:
package mainimport"fmt"func main() { varint17 varint5 varfloat32 float32(sum)/float32(count) fmt.Printf("mean 的值为: %f\n",mean)}
The result of the above example output is:
3.400000
Go Language Interface
The Go language provides another type of data interface, which defines all the common methods, and any other type implements this interface as long as it implements these methods.
Instance
/* 定义接口 */typeinterface { method_name1 [return_type] method_name2 [return_type] method_name3 [return_type] ... method_namen [return_type]}/* 定义结构体 */typestruct { /* variables */}/* 实现接口方法 */func (struct_name_variable struct_name) method_name1() [return_type] { /* 方法实现 */}...func (struct_name_variable struct_name) method_namen() [return_type] { /* 方法实现*/}
Instance
package mainimport ( "fmt")typeinterface { call()}typestruct {}func (nokiaPhone NokiaPhone) call() { fmt.Println("I am Nokia, I can call you!")}typestruct {}func (iPhone IPhone) call() { fmt.Println("I am iPhone, I can call you!")}func main() { var phone Phone new(NokiaPhone) phone.call() new(IPhone) phone.call()}
In the example above, we define an interface phone with a method call () inside the interface. Then we define a phone type variable in the main function and assign it to Nokiaphone and iphone, respectively. The call () method is then called and the output is as follows:
I am Nokia, I can call you!I am iPhone, I can call you!
Go error handling
The go language provides a very simple error handling mechanism via the built-in error interface, which is defined as an interface type:
typeerrorinterface { string}
We can generate error information in the encoding by implementing the wrong interface type, which usually returns an error message in the final return value, using error. New can return an error message:
funcfloat64) (float64error) { if0 { return0, errors.New("math: square root of negative number") } // 实现}
In the following example, we pass a negative number when we call sqrt, and then we get the Non-nil Error object, comparing this object to nil, and the result is true, so fmt. Printin (The FMT package calls the error method when handling error) is called to output an error, see the sample code called below:
result, err := Sqrt(-1)ifnil { fmt.PrintIn(err)}
Instance
PackageMainImport("FMT")//define a DIVIDEERROR structuretypeDivideerrorstruct{DivideeintDividerint}//Implement ' Error ' interfacefunc(De *divideerror) Error ()string{Strformat: =`cannot proceed, the divider is zero.Dividee:%ddivider:0` returnFmt. Sprintf (Strformat, De.dividee)}//define the function of ' int ' type division operationfuncDivide (Vardivideeint, Vardividerint) (Resultint, errormsgstring) {ifVardivider = =0{dData: = divideerror{Dividee:vardividee, Divider:vardivider,} errormsg = Ddata.error ()return}Else{returnVardividee/vardivider,""}}funcMain () {//Normal condition ifresult, ErrorMsg: = Divide ( -,Ten); ErrorMsg = =""{FMT. Println ("100/10 =", result)}//When the divisor is zero, an error message is returned if_, ErrorMsg: = Divide ( -,0); ErrorMsg! =""{FMT. Println ("ErrorMsg is:", ErrorMsg)}}
The results of the above example run as follows:
100/10 = 10errorMsg is: Cannot proceed, the divider is zero. 100 0
At this point, the basic part of the go language has been shared, and will continue to share in-depth parts of the go language, please look forward to ...
Go Language "14th": the basic summary of Go language