Go Language Practical Notes (vii) | Go type

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

"Go language Combat" reading notes, not to be continued, welcome to sweep code attention flysnow_org to the public, the first time to see follow-up notes.

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 types are the types of the go language, such as numeric types, floating-point types, character types, and Boolean types, which are essentially primitive types, which are immutable, so manipulating them generally returns a newly created value, so when you pass these values to a function, you actually pass a copy of the value.

12345678910
func Main ()  {name:="Zhang San"FMT. Println (Modify (name)) FMT. PRINTLN (name)}funcModifystring)string{s=s+s  return S}
12
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, reference types have slices, maps, interfaces, function types, and 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.

12345678910
func Main ()map[string]int{"Zhang San"}fmt. Println (Ages) Modify (Ages) fmt. Println (Ages)}funcModifymap[string]int) {m["Zhang San"Ten}

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.

1234
type struct int 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.

1
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.

1
Jim: = person{,"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.

1
Jim: = Person{name:"Jim", Age:Ten}

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.

123456789101112131415
func Main ()  {jim: = person{,"Jim"}fmt. Println (Jim) Modify (Jim) fmt. Println (Jim)}funcmodify(P person)  {p.age =p.age+}type  structintstring}

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

123456789101112131415
func Main ()  {jim: = person{,"Jim"}fmt. Println (Jim) Modify (&jim) fmt. Println (Jim)}funcmodify(P *person)  {p.age =p.age+}type  structintstring}

The output of this example is

12
{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.

1
type 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.

1234
type Int64 var  - var Int64  -

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

12345
type Int64 var dur durationdur=Int64(+) fmt. Println (dur)

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

1
Cannot use Int64 (typetype 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, we can use a custom type to do a lot of things, such as adding methods, such as can more clearly represent the meaning of the business and so on, the next method we will talk about.

"Go language Combat" reading notes, not to be continued, welcome to sweep code attention flysnow_org to the public, the first time to see follow-up notes.

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.