Go programming language Getting Started Tutorial (ii)

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

Original: http://golang.org
Translation: Liu Jinhu/Liu Yuntao <yuntao.liu#gmail.com> http://www.log4think.com

Data type Types


Go has some common data types, such as int and float, whose values are represented by the size of the machine "fit". There are also defined size data types, such as int8, float64, and unsigned integer types such as uint, UInt32, and so on. These are completely different data types, even if both int and Int32 are 32-bit integers, but they are different types. The same is true for types that represent string elements, byte and uint8.
Speaking of string, this is also a built-in data type. The value of a string is not just an array of byte, its value is immutable. Once you have determined the value of a string, you can no longer modify it. However, the value of a string variable can be changed by re-assigning the value. The following code from STRINGS.GO is legal:

One s: = "Hello";
If s[1]! = ' E ' {os. Exit (1)}
s = "Good Bye";
var p *string = &s;
*p = "Ciao";
However, the following code is illegal because it attempts to modify the value of a string:

S[0] = ' x ';
(*p) [1] = ' Y ';
According to C + +, Go's string is somewhat similar to a const modifier, and a pointer to a string is similar to a reference to a const string (reference).
Yes, those are pointers, but pointers in the go language are simplified in terms of usage, as mentioned later.
The declaration of the array is as follows:

var arrayofint [10]int;
Arrays are "values" as strings, but they are mutable. Unlike C, Arrayofint can be used as a pointer to an int in the C language. In go, because the array is a "value", Arrayofint is considered (also used as) a pointer to an array.
The size of the array is part of its data type. However, you can declare a slice variable, and then you can assign a value to it with an array pointer to the same element type, more commonly a slice expression with a form of A[low:high], which represents a sub-array of subscripts from low to high-1. The Slice type resembles an array, but does not explicitly specify a size ([] to [10]), which is used to represent an implicit (usually anonymous) array. If different slice represent data in the same array, they can share the memory of the array, but the different arrays never share the memory data.
Slice is more common in go programs than arrays. It is more flexible, has referential semantics, and is more efficient. The disadvantage is that you cannot control storage exactly as you would an array, and if you want to save a sequence of 100 elements in a data structure, you should use an array.
When passing an array parameter to a function, the parameter is declared in most cases as the slice type. When the function is called, the address of the array is taken first, then go creates a slice reference and then passes the reference past.
You can use slice to write this function (from Sum.go):


The]int func sum (a [) int {//returns an integer
Ten s: = 0;
One for I: = 0; I < Len (a); i++ {
s + = A[i]
13}
return s
15}
This is followed by a call:


s: = SUM (&[3]int{1,2,3}); A slice of the array is passed to sum
Note that the return value type (int) is defined with an int after the argument list of sum (). [3]int{1,2,3} is in the form of a data type followed by a brace-enclosed expression, and the entire expression constructs a value, here is an array of three integers. The previous & represents the address where this value is extracted. This address is implicitly converted to a slice to sum ().
If you want to create an array, but want the compiler to help you determine the size of the array, you can use ... As an array size:

s: = SUM (&[...] int{1,2,3});
In practice, the slice itself (with [] without &) is sufficient unless it is very concerned about how the data structure is stored:

s: = SUM ([]int{1,2,3});
In addition to the map, you can initialize this:

M: = map[string]int{"One": 1, "One": 2}
Sum also appears for the first time with the built-in function Len (), which returns the number of elements. Can be used for strings, arrays, slice, maps, maps, and channel.
In addition, the range in the For loop can also be used for strings, arrays, slice, maps, maps, and channel. For example

For I: = 0; I < Len (a); i++ {...}
Traversing each element of a sequence can be written as

For I, V: = Range A {...}
Where I will be assigned the subscript, V will be assigned to the corresponding value in a, effective Go contains more usage demos.

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.