Learning Golang Language (7)

Source: Internet
Author: User
Tags install go
This is a creation in Article, where the information may have evolved or changed.
1. Methods

Go has no class. However, you can still define a method on a struct type.

The method recipient appears in a parameter between the Func keyword and the method name.


2. Method (cont.)

You can define any method for any type in the package, not just for the struct.

However, you cannot define a method on a type or an underlying type from another package.


3. The receiver is the pointer method

Method can be associated with a pointer to a named type or a named type.

Just saw the two Abs method. One is on the *vertex pointer type and the other on the Myfloat value type. There are two reasons to use a pointer recipient. First, avoid copying values in each method call (more efficient if the value type is a large struct). Second, the method can modify the value that the receiver points to.

Try to modify the definition of Abs, while the scale method uses Vertex instead of *vertex as the receiver.

The scale method has no effect when V is Vertex. ' Scale ' modifies ' V '. When V is a value (not a pointer), the method sees a copy of Vertex and cannot modify the original value.

The way Abs works is the same. Just read ' V '. So it doesn't matter whether the original value (through the pointer) or the copy of the value is read.


4. Interface

An interface type is a collection defined by a set of methods.

The value of the interface type can hold any value that implements these methods.

Note: There is an error in the 22 line of the code. Because ABS is defined only on *vertex (pointer type), Vertex (value type) does not meet ' abser '.


5. Implicit interface

Types implement interfaces by implementing those methods. There is no need for explicit declaration, so there is no keyword "implements".

Implicit interfaces decouple the package that implements the interface and the package that defines the interface: non-dependent.

Therefore, there is no need to add a new interface name on each implementation, which also encourages explicit interface definitions.

Package IO defines Reader and ' Writer ', but not necessarily.


6, stringers

A ubiquitous interface is the Stringer defined in the FMT package.

Type Stringer struct {
String () string
}

Stringer is a type that can be used to describe itself as a string. The ' FMT ' package (and many other packages) uses this to output.


7. Error

The GO program uses the error value to indicate the status of the fault.

With FMT. Stringer similar, the ' error ' type is a built-in interface:

Type Error Interface {
Error () string
}

(with FMT.) Stringer similar, the ' FMT ' package will also attempt to match ' error ' on output. )

Normally the function returns an error value, and the code that invokes it should determine if the error equals ' nil ' to handle the error.

I, err: = StrConv. Atoi ("42")
If err! = Nil {
Fmt. Printf ("couldn ' t convert number:%v\n", err)
}
Fmt. Println ("Converted integer:", i)

The error is nil when it is successful, and non-nil errors indicate.


8, Readers


IO package Specifies IO. Reader interface, which represents reading from the end of the data stream.

The Go standard library contains many implementations of this interface, including files, network connections, compression, encryption, and so on.

Io. The Reader interface has a Read method:

Func (T) Read (b []byte) (n int, err error)

Read populates the specified byte slice with data, and returns the number of bytes populated and the error information. Returns IO when the end of the data stream is encountered. EOF error.

The example code creates a strings. Reader. and reads its output at a rate of 8 bytes at a time.


9. Web Server

The package HTTP implements HTTP through any. The Handler value to respond to the HTTP request:

Package HTTP

Type Handler Interface {
Servehttp (w responsewriter, R *request)
}

In this example, the type Hello implements the ' HTTP '. Handler '.

Visit Http://localhost:4000/to see Greetings from the program.

Note: This example cannot be run in the Web-based guide user interface. In order to attempt to write a Web server, you may need to install Go.


10. Pictures

The package image defines the image interface:

Package image

Type Image Interface {
ColorModel () color. Model
Bounds () Rectangle
at (x, y int) color. Color
}

* Note *: The Rectangle return value of the ' Bounds ' method is actually an image. Rectangle, which is defined in the image package.

Color. Color and color. The Model is also an interface, but usually because the predefined implementation image is used directly. RGBA and image. Rgbamodel and neglected. These interfaces and types are defined by the Image/color package.

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.