"Go Language" "4" Go language type and add method for type

Source: Internet
Author: User
Tags sublime text

First, the type of Go language

The following basic types are built into the go language:

Integer: Byte (int8), int16, int, uint, etc.

Floating-point type: float32, float64

Plural type: complex64, complex128

Boolean type: BOOL

Character Type: Rune

Strings: String

Fault Type: Error

The go language also supports the following composite types:

Pointer

Array

Slices: Slice

Collection: Map

Channel: Chan

struct: struct

Interface: interface


Look at the left look at the right look, how do not see the double type ? At the same time there are several unfamiliar faces, like Rune, error, Slice, Chan and so on, oh, go does not provide a double type, it is considered that the floating-point type is sufficient to express the range of double, so the double is canceled;

In addition, the integral type is also divided, such as int8, int16, int, uint, uintptr, and so on, where int8 and byte is equivalent, you can think of Byte is the alias of Int8, to the type of alias but go to the special ability yo ~ ~ ~


1, according to Java habits to create workspace directory, I put this workspace into the GOROOT environment variable, and then create SRC, bin, pkg three directories

2. Open Sublime text and select "File > Open Folder ..."

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/59/C7/wKioL1TiAcDQHJH6AAG7seiSLsU427.jpg "title=" open Floder.png "alt=" Wkiol1tiacdqhjh6aag7seislsu427.jpg "/>

3. Select the workspace directory in the pop-up window

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/59/CA/wKiom1TiASHB-a3sAAJ2KHXpeq8293.jpg "title=" Select Workspace.png "alt=" Wkiom1tiashb-a3saaj2khxpeq8293.jpg "/>

4. Enter the following code in the sublime text

Package Main

Import "FMT"

Type Intnick int//Intnick alias for int

Func Main () {
var a intnick
Fmt. Println (a)
}

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/59/C7/wKioL1TiAy-CFkzTAAH9UYHlkeY812.jpg "title=" write Code.png "alt=" Wkiol1tiay-cfkztaah9uyhlkey812.jpg "/>

5. Select "File > Save with Encoding > UTF-8" and file name as "Nick.go"

6.type Intnick int means an alias is taken as int, called Intnick

var a intnick defines a variable a whose type is Intnick and the default value is 0

FMT. Println (a) indicates the value of the output variable a

7, press CTRL + B, enter go run nick.go after the return, you can see the result: output 0, time consuming 1.0231298 seconds

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/59/CA/wKiom1TiBRrz-3H3AAIKv42L1Xw211.jpg "title=" out Result.png "alt=" Wkiom1tibrrz-3h3aaikv42l1xw211.jpg "/>


Ii. ways to increase the type

In the above example it is not just an alias, but a redefinition of a type, we can add a method for this new type

1, add the following code in the Nick.go

/**
* Add a Islessthanparam method to the Intnick type
*/
Func (Nick Intnick) Islessthanparam (param Intnick) bool {
Return Nick < param
}

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/59/CA/wKiom1TiCKWREQZ6AAJNmWu_GUM836.jpg "title=" add Method.png "alt=" Wkiom1tickwreqz6aajnmwu_gum836.jpg "/>

2. Method definition of Go language

Func methodname (param1 paramType1, param2 paramType2) (Result1 resultType1, result2 resultType2) {

//... Slightly

}

Where func is the method modifier

MethodName is the method name

param1,param2 is the method to enter the parameter

paramType1,paramType2 is the parameter of the method entry parameters

result1,result2 is the return value

resultType1,resultType2 is the type of return value

"Remarks":

What's interesting about the go language is that it can return multiple values, which programmers have always dreamed of, but not supported in other languages.

In the [3]go language constants and variables, said: Go language is very consistent with people's normal thinking, code to write how to write, no matter how the writing is in line with the grammar of Go.

2.1 If param1 and param2 are the same type, they are paramtype, so it is natural to abbreviate the method to

Func methodname (param1,param2 paramtype) (Result1 resultType1, result2 resultType2) {

//... Slightly

}

2.2 If RESULT1 and RESULT2 are the same type, they are resulttype, then the method can be shortened to

Func methodname (param1, param2 paramtype) (result1, result2 resulttype) {

//... Slightly

}

2.3 If the parameter and the return value have only one, then the method can be shortened to

Func methodname (param paramtype) (result Resulttype) {

//... Slightly

}

2.4 Since there is only one return value, return the type of the return value directly, then the method can be shortened to

Func methodname (param paramtype) resulttype{

//... Slightly

}

The above expression is correct, so the go language is simple and not simple

3. The difference between the method of increasing type and the general method

How does the Go language define methods in 2, and what is the difference between the new types of methods we define and the general methods?

    • To add a method to a type:

Func (Nick Intnick) Islessthanparam (param Intnick) Boolean {

Return Nick < param

}

    • General Method:

Func methodname (param paramtype) resulttype{

//... Slightly

}

Discerning eye it out--added to the method name (Nick Intnick), which means that the method is for the Nick object , the type of theNick object is Intnick.

So when you use this method in main, you can encode it in terms of an object-oriented approach:

Defines object A, which is an object of type Intnick. It's like having an integer class whose instance is a
var a intnick = 8
Call the Islessthanparam () method of Object A
A.islessthanparam (9)

4, Go is the process-oriented, or object-oriented, or function-oriented?

This is a good topic, in my opinion it has, in the 51cto video teaching, no-smell teacher mentioned that go is a type-oriented programming, a very incisive induction.

5. Modify the Main method

Func Main () {
var a intnick = 8
Fmt. Println (A.islessthanparam (9))
}

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/59/CB/wKiom1TiEHWQtr61AAKOsAf9V0U172.jpg "title=" Modify Main.png "alt=" Wkiom1tiehwqtr61aakosaf9v0u172.jpg "/>

6, press CTRL + B, enter go run nick.go after the return, you can see the result: output True

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/59/CB/wKiom1TiELPiMFxSAAJ0QoVo-rg843.jpg "title=" method Result.png "alt=" Wkiom1tielpimfxsaaj0qovo-rg843.jpg "/>


Third, off-topic

1. Although Byte is an alias of int8, it is not recommended to use int8 to specify the type of the variable in the general development process, which can easily lead to migration difficulties

2,go language to the code arrangement also carries on the grammar level examination, the hand writing above code will discover, the function definition when the curly brace must be behind this line, if the curly brace new row, then will appear unexpected semicolon or NewLine before { error, as follows:


{
Return Nick < param
}

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/59/C7/wKioL1TiEzuyC-yRAALIMKDdl3M070.jpg "title=" format Error.png "alt=" Wkiol1tiezuyc-yraalimkddl3m070.jpg "/>


This article is from the "Green Guest" blog, please be sure to keep this source http://qingkechina.blog.51cto.com/5552198/1614711

"Go Language" "4" Go language type and add method for type

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.