38. Toad Notes Go language-advanced

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

38. Toad Notes Go language-advanced

Go has pointers, but there is no pointer operation. You cannot iterate through the individual bytes of a string with a pointer variable. So they are more like references than the pointers you know from C. Pointers are very useful.

Go is also garbage collected, meaning there is no need to worry about memory allocation and recycling.

Go has two memory allocation primitives, new and make. They apply to different types, do different jobs, and may be confusing, but the rules are simple.

Allocating memory with new

The built-in function new is essentially the same as the function function of the same name in other languages: new (t) allocates a 0 value to fill the memory space of type T, and returns its address, a value of type *t. In the terms of go, it returns a pointer to the 0 value of the newly assigned type. One thing is very important: new returns a pointer.

This means that the user can create an instance of the data structure with new and can work directly.

allocating memory with Make

Back to memory allocation. The built-in function make (T,args) has different functions from new (T). It can only create slice,map and channel, and returns a T type with an initial value (not 0) instead of *t. Essentially, the reason that these three types are different is that references to data structures must be initialized before they are used.

Make returns the initialized (not 0) value.

Make sure that make is only available for Map,slice and channel, and is not a pointer returned. A specific pointer should be obtained with new.

New (T) returns *t pointing to a 0 value T

Make (t) returns the initialized T

Define your own type

Go allows you to define a new type, which is implemented by the reserved word type

Type Foo int

A new type Foo is created with the same function as int. Creating more complex types requires the use of struct reserved words. Here's one. Record a person's name (string) and age (int) in a data structure

Package Main

Import "FMT"

Type nameage struct {

Namestring

Age int

}

Func Main () {

A: =new (nameage)

A.name= "Pete"

A.age =42

Fmt. Printf ("%v\n", a)

}

Perform:

&{pete42}

Transformation

Sometimes a type needs to be converted to another type. You can do it in go, but there are some rules. First, converting one value to another is done by the operator (which looks like a function: Byte ()), and not all conversions are allowed.

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.