GO programming for Java developers 3

Source: Internet
Author: User

Slices (slice) slice is a conceptual structure that contains three fields: the pointer, length, and capacity of an array. Slice supports the [] operator to access the elements of the underlying array. The length of the slice returned by the built-in len function. The built-in cap function returns slices. For an array or another slice, use a [I: j] to create a new slice. The newly created slice points to a, starting from index I and before the index j. Its length is j-I. If I is omitted, the slice starts from 0. If j is omitted, the slice ends at len (. The new slice points to the same Array like. That is, the elements of the new slice formed after the change can be seen in. The capacity of the new slice is simply a minus I. The size of the array is the length of the array. Var s [] intvar a [10] int s = a [:] // short for s = a [0: len (a)] if you create a value type of [100] byte (100 bytes, maybe an array of buffer), you want to pass it to the function instead of copying it, the parameter declaration type of the function is [] byte, And the array slices are passed in. You can also use the make function to create a slice (as described below ). The Slice combination uses the built-in append function. The Java ArrayList provides the same functions. S0: = [] int {1, 2} s1: = append (s0, 3) // append a single elements2: = append (s1, 4, 5) // append multiple elementss3: = append (s2, s0 ...) // append a slice slicing syntax, which can also be used on strings. It returns a new string with the value of the make function Map and channel values must be allocated using the built-in function make. for example, calling map and channel values must use the built-in function make. For example, call make (map [string] int) map [string] int to return a new value type. Compared with new, make returns the actual object instead of an address. This is the same fact. map and channel are reference types. For map, the make function regards the capacity as an optional second parameter. For a channel, there is an optional second parameter to set the channel buffer capability. The default value is 0 (no buffer ). The make function can also be used to allocate a slice. In this case, it allocates memory to the basic array and returns a slice that references it. The number of elements in the slice is a required parameter. The second parameter is the slice capacity. M: = make ([] int, 10, 20) // Same as new ([20] int )[: 10] Methods and interface methods look like a common function definition, but they have a receiver ). Extends er is similar to this reference in Java instance methods. Type MyType struct {I int} func (p * MyType) Get () int {return p. i} var pm = new (MyType) var n = pm. get () declares that the Get method is associated with MyType. The handler is named p in the function body. To define a method. If you convert values of different types, new values will have new types instead of old ones. You can define a built-in type method and use the new naming type declaration. The new and built-in types are different. Type MyInt int func (p MyInt) Get () int {return int (p) // The conversion is required .} func f (I int) {} var v MyInt v = v * v // The operators of the underlying type still apply. f (int (v) // int (v) has no defined methods. f (v) // The Go interface of the INVALID interface is similar to the Java interface, but it can be considered as a method to implement this interface to provide any type of Go interface naming. Explicit statements are unnecessary. Interface is like this: type MyInterface interface {Get () int Set (I int)} since MyType already has the Get method, we can make MyType meet the interface by adding func (p * MyType) set (I int) {p. I = I} Now any variable that uses MyInterface as a parameter can receive the variable func GetAndSet (x MyInterface) whose type is * MyType {} func f1 () {var p MyType GetAndSet (& p)} in Java terminology, define Set and Get for * MyType to enable * MyType to automatically implement the MyInterface interface. This type can meet multiple interfaces. This is a duck type. "When you see a bird walking like a duck, swimming like a duck, or screaming like a duck, this bird can be called a duck ." James Whitcomb Riley anonymous domain can be used to implement something similar to a Java subclass. Type MySubType struct {MyType j int} func (p * MySubType) Get () int {p. j ++ return p. myType. get ()} MySubType is effectively implemented as a child type of MyType. the func f2 () {var p MySubType GetAndSet (& p)} Set method inherits from MyType, because the method associated with the anonymous domain is changed to the closed type method. In this case, because MySubType has an anonymous and domain MyType type, the MyTypee method also becomes the MySubType method. The Get method is overwritten, And the Set method is inherited. This is not exactly the same as the subclass in Java. When a method of an anonymous domain is called, its receiver is the anonymous domain, rather than the surrounding struct. In other words, methods in the anonymous domain are not dynamically scheduled. If you want to implement dynamic method search equivalent to Java, use the interface. Func f3 () {var v MyInterface v = new (MyType) v. get () // Call the Get method for * MyType. v = new (MySubType) v. get () // Call the Get method for * MySubType .} type assertions use a type assertion to convert a variable with the same interface type to a variable with different interface types. This is executed dynamically at runtime. Unlike Java, there is no need to declare the relationship between the two interfaces. Type Printer interface {Print ()} func f4 (x MyInterface) {x. (Printer). Print () // type assertion to Printer}

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.