"Go Language" Go type: Basic type, reference type, struct type, custom type

Source: Internet
Author: User

The Go language is a static type of programming language, so when the compiler compiles, you need to know the type of each value so that the compiler knows how much memory to allocate for that value and knows what the allocated memory represents.

There are many benefits to knowing the type of value in advance, such as the compiler can use these values reasonably, can further optimize the code, improve the efficiency of execution, reduce bugs and so on.

Basic type

The basic type is the type of the Go language, such as numeric , floating point , string , boolean , array , and error types, which are essentially primitive types, That is immutable, so manipulating them will generally return a newly created value, so when you pass these values to a function, you actually pass a copy of the value.

Funcmain () {name:= "Zhang San" fmt. Println (Modify (name)) FMT. PRINTLN (name)}funcmodify (sstring) String{s=s+sreturn s}
Zhang Sanzhang

The above is an example of an operation string, through the printed result, you can see that name the original value has not been changed, that is, we pass a copy of the time, and return a newly created string.

The base type is a copy of the value, and when you manipulate it, the generated value is also newly created, so these types are safe in multi-line thread, and we don't have to worry about the modification of one thread that affects the data of another thread.

Reference type

The reference type is the opposite of the original primitive type, and its modification can affect any variable that is referenced to it. In the Go language, the reference type has a slice , a dictionary (map), an interface , a function type , and a channel ( chan) .

Reference types can be referenced because we create a variable of reference type, which is actually a header value, and the header value contains a pointer to the underlying data structure, and when we pass the reference type in the function, we actually pass a copy of the header value, and the underlying structure it points to is not copied. This is also why reference type delivery is efficient.

Essentially, we can understand that the transfer of a function is a value pass, except that the reference type passes a pointer to the underlying data, so we can modify the value of the underlying data shared at the time of the operation, which in turn affects all variables referenced to the shared underlying data.

Funcmain () {Ages: = map[string]int{"Zhang San": 20}fmt. Println (Ages) Modify (Ages) fmt. Println (Ages)}funcmodify (mmap[string]int) {m["Zhang san"] = 10}

This is an obvious example of modifying a reference type, and modifying the function modify will affect the value of the original variable ages .

Structure type

A struct type is used to describe a set of values, such as a person's height, weight, name, and age, which is essentially an aggregated type of data.

Type person struct {age Intname string}

To define the type of a struct, declare it by type keyword and type, struct we define a struct type person , which has age name both field data.

Once the struct type is defined, it can be used, and we can var declare a struct-type variable with a keyword.

var p person

The way this is declared is the person default initialization of data types in structs, that is, using their type of 0 values, which is most commonly used if you want to create a struct variable and initialize it to a zero value var .

If we need to specify a value other than 0, we can use our literal method.

Jim: = person{10, "Jim"}

Example of this we specify a value for it, note that the order of this value is important, must be in the structure of the Declaration of the Order of the fields, of course, we can also do not order, but at this time we must specify a value for the field.

Jim: = Person{name: "Jim", Age:10}

Use colons : to separate field names and field values so that we don't have to strictly follow the defined order.

In addition to the basic primitive types, the values in the structure body can be reference types, or other types that you define yourself. Select the type, depending on the actual situation, such as whether to allow the modification of the value itself, if allowed, you can choose the reference type, if not allowed, you need to use the basic type.

Function arguments are value-passing, so it is no exception to structs that the struct passes itself and copies of the values inside it.

Funcmain () {jim: = person{10, "Jim"}fmt. Println (Jim) Modify (Jim) fmt. Println (Jim)}funcmodify (P person) {P.age =p.age+10}type person struct {age Intname string}

The output of the above example is the same, so we can verify that a copy of the value is passed. If the above example we want to modify age the value can pass through the structure of the pointer, we slightly change the following example

Funcmain () {jim: = person{10, "Jim"}fmt. Println (Jim) Modify (&jim) fmt. Println (Jim)}funcmodify (P *person) {p.age =p.age+10}type person struct {age Intname string}

The output of this example is

{Ten Jim} {Jim}

It is very obvious that age the value has been changed. If there is a reference type value in the struct, for example, if we pass a copy of the value of the map struct, if we modify this map , the corresponding value of the original structure map will be modified, and the example is not written here, you can verify it.

Custom Types

The Go language supports our custom types, such as the type of struct we have just above, which is our custom type, which is also a common method of custom types.

Another way to customize a type is to create a new type based on an existing type, which is also used as a type keyword.

Type Duration Int64

When we use time this package, we time.Duration should be very familiar with the type, which is actually based on the int64 new type created by this primitive type, to represent the interval of time.

But here we note that, although Duration based on the int64 creation, we feel that they are actually the same, for example, you can use a numeric value assignment.

Type Duration int64var I Duration = 100var j Int64 = 100

But in essence, they are not the same type, so for the strong-typed language of Go, they cannot assign value to each other.

Type Duration int64var dur Durationdur = Int64 (+) FMT. Println (dur)

The above example, at compile time, will report the type conversion exception error.

Cannot use Int64 (+) (type Int64) as type Duration in assignment

The Go compiler doesn't do implicit type conversions like Java does.

Sometimes, people will be confused, already have int64 these types, can say, but also based on what they create a new type to do? In fact, this is where Go is flexible, and we can use custom types to do a lot of things, such as adding a method, such as a clearer representation of what the business means, and so on.

Go language Combat Go type: Basic type, reference type, struct type, custom 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.