Go Series Tutorial--4. Type

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. This is the 4th tutorial of our [Golang Series Tutorial] (/SUBJECT/2). Read the [Golang Tutorial part 3rd: variables] (/articles/11756) in this series to learn about variable knowledge. The following are the basic types of Go support:-bool-number Types-Int8, Int16, Int32, Int64, Int-uint8, UInt16, UInt32, UInt64, Uint-float32, float64-comple x64, complex128-byte-rune-string### Boolbool type represents a Boolean value of TRUE or false. "' Gopackage mainimport" FMT "Func Main () {A: = true b: = False fmt. Println ("A:", A, "B:", b) c: = a && b fmt. Println ("C:", c) d: = a | | FMT B. Println ("D:", D)} "[Online Run Program] (Https://play.golang.org/p/v_W3HQ0MdY) in the above program, a assignment value of true,b assigned to false. C is assigned a && B. The operator && returns true only if both A and B are true. Therefore, C is false here. When a or B is true, the operator | | Returns True. Here, because A is true, D is also true. We will get the output of the program as follows. ' A:true b:false c:false d:true ' # # # signed integer **int8**: Represents a 8-bit signed integer * * size **:8 bit * * Range **:-128~127**int16**: Indicates 16-bit signed integer * * Large Small **:16 bit * * Range **:-32768~32767**int32**: Represents a 32-bit signed integer * * size **:32 bit * * Range **:-2147483648~2147483647**int64**: 64-bit signed integer * * Size * *: 64-bit * * Range **:-9223372036854775808~9223372036854775807**int**: Represents a 32-or 64-bit integer based on a different underlying platform (underlying Platform). Unless you have a specific need for the size of an integral type, you should typically use *int* to represent an integral type. * * Size * *: 32 bits under 32-bit system and 64-bit under 64-bit system. * * Range * *: Under 32-bit systems is -2147483648~2147483647, while on 64-bit systems is -9223372036854775808~9223372036854775807. "' Gopackage mainimport" FMT "Func Main () {var a int =" a "): =---" FMT ". Println ("Value of A is", A, "and B is", B)} "[Online Run Program] (HTTPS://PLAY.GOLANG.ORG/P/NYDPSJKMA3) above the program will output ' value of a is the" B is 95 '. In the above program, A is an int type, and the type of B is inferred from the assignment (95). As we mentioned above, the size of the int type is 32 bits under the 32-bit system, and 64 bits under the 64-bit system. Next we will confirm this statement. In the Printf method, you can print out the type of the variable by using the **%t** format specifier (formatted specifier). Go's [unsafe] (https://golang.org/pkg/unsafe/) package provides a [Sizeof] (https://golang.org/pkg/unsafe/#Sizeof) function, The function receives the variable and returns its byte size. The *unsafe* package should be used with caution, as the use of unsafe packages may introduce portability issues. However, for the purposes of this tutorial, we can use it. The following program outputs the types and sizes of variables A and B. The format specifier '%T ' is used for the print type, and '%d ' is used to print the byte size. "' Gopackage mainimport (" FMT "" unsafe ") Func main () {var a int = B: = the FMT. Println ("Value of A is", A, "and B is", b) fmt. Printf ("Type of A is%T, size Of A is%d ", a, unsafe. Sizeof (a))//A of the type and size of FMT. Printf ("\ntype of B is%T, size of B is%d", B, unsafe. Sizeof (b))///b type and size} "[Online Run Program] (HTTPS://PLAY.GOLANG.ORG/P/MFSMJVK5OC) above the program will output:" ' Value of A is a "and B is a. is int, size of a are 4 type of B is int., size of B is 4 "" from the above output, we can infer that A and B are *int* types, and the size is 32 bits (4 bytes). If you run the above code on a 64-bit system, there will be different outputs. Under 64-bit systems, A and B consume 64 bits (8 bytes) of size. # # # unsigned integer **uint8**: Represents a 8-bit unsigned integer * * size **:8 bit * * Range **:0~255**uint16**: represents 16-bit unsigned integer * * size **:16 bit * * Range **:0~65535**uint32**: 32-bit unsigned integer * * size **:32 bit * * Range **:0~4294967295**uint64**: Indicates 64-bit unsigned integer * * size **:64 bit * * Range **:0~18446744073709551615**uint** : Represents a 32-or 64-bit unsigned integer based on a different underlying platform. * * Size * *: 32 bits under 32-bit system and 64-bit under 64-bit system. * * Range * *: Under 32-bit systems is 0~4294967295, while on 64-bit systems is 0~18446744073709551615. # # # floating point **float32**:32 bit floating point **float64**:64 bit floating point number The following simple procedure demonstrates the use of integral and floating-point types. "' Gopackage mainimport (" FMT ") func main () {A, B: = 5.67, 8.97 fmt. Printf ("Type of a%T b%t\n", A, b) sum: = a + b diff: = A-a FMT. Println ("sum", Sum, "diff", diff) No1, NO2: =, the FMT. The types of Println ("sum", No1+no2, "diff", No1-no2)} "[Online Run Program] (HTTPS://PLAY.GOLANG.ORG/P/UPWUCPRT-J) A and B are inferred from the assignment. Here, the type of A and B is float64 (Float64 is the default type for floating-point numbers). We assign a and B to the variable sum, assign the difference between B and a to diff, and then Print Sum and diff. No1 and NO2 also perform the same calculations. The above program will output: ' ' type of a float64 b float64 sum 14.64 diff-3.3000000000000007 sum 145 diff-33 ' ' # # # plural type **complex64**: real and imaginary Are the plural of the float32 type. **complex128**: Both the real and imaginary parts are complex numbers of the float64 type. The built-in function [**complex**] (https://golang.org/pkg/builtin/#complex) is used to create a complex number that contains both real and imaginary parts. The complex function is defined as follows: "' Func complex (R, I floattype) ComplexType '" The function's arguments are real and imaginary, and return a plural type. The real and imaginary parts should be of the same type, i.e. float32 or float64. If both the real and imaginary parts are float32 types, the function returns a complex number of type complex64. If both the real and imaginary parts are float64 types, the function returns a complex number of type complex128. You can also use the short syntax to create complex numbers: "' c: = 6 + 7i" Below we write a simple program to understand complex numbers. "' Gopackage mainimport (" FMT ") func main () {c1: = Complex (5, 7) C2: = 8 + 27i Cadd: = c1 + C2 FMT. Println ("Sum:", cadd) Cmul: = C1 * C2 FMT. PRINTLN ("Product:", Cmul)} "[Running Program Online] (https://play.golang.org/p/kEz1uKCdKs) in the program above,C1 and C2 are two complex numbers. The real part of the C1 is 5 and the imaginary part is 7. The real part of the C2 is 8 and the imaginary part is 27. The sum of C1 and C2 is assigned to ' Cadd ', while the product of C1 and C2 is assigned to ' Cmul '. The program will output: ' Sum: (13+34i) Product: ( -149+191i) ' # # # # # # # # Other numeric type **byte** is the alias of Uint8. **rune** is the alias of Int32. We will discuss byte and rune in detail when we learn the string. # # # String type in Golang, strings are a collection of bytes. If you don't understand the definition now, it doesn't matter. We can assume for a moment that a string is made up of many characters. We'll learn more about strings in a tutorial later. Write a program that uses a string. "Gopackage mainimport (" FMT ") func main () {first: =" Naveen "Last: =" Ramanathan "Name: = First +" "+ Last FMT. Println ("My name is", name)} "[Online Run Program] (Https://play.golang.org/p/CI6phwSVel) above program, first assignment is the string" Naveen ", Last assignment is a string "Ramanathan". The + operator can be used to stitch strings. We spliced the first, the space, and the last, and assign the value to name. The above program will print out ' My name is Naveen Ramanathan '. There are many actions that apply to the string above, and we will see them in a separate tutorial. # # # type conversion Go has a very strict strong-type feature. Go does not have automatic type promotion or type conversion. We use an example to illustrate what this means. "' Gopackage mainimport (" FMT ") func main () {i: =//int J: = 67.8//float64 Sum: = i + j//do not allow int + float64 FMT. Println (SUM)} "[Online Run Program] (HTTPS://PLAY.GOLANG.ORG/P/M-TMRPMMNM) above the code in the C language is completely legal, but in Go, but it is not feasible. The type of i is int, and the type of J is float64 , we are trying to add two different types of numbers, and Go does not allow such operations. If you run the program, you will get ' Main.go:10:invalid operation:i + j (mismatched types int and float64) '. To fix this error, I and J should be the same type. Here, we convert j to int type. The syntax for converting V to type T is T (v). "Gopackage mainimport (" FMT ") func main () {i: =//int J: = 67.8//float64 Sum: = i + int (j)//j is converted to int Fmt. Println (SUM)} "[Running Program Online] (https://play.golang.org/p/mweu3n3jMy) Now, when you run the above program, you will see output ' 122 '. The same is true of the assignment. Assigning a variable to another variable of a different type requires an explicit type conversion. This is illustrated in the following procedure. "' Gopackage mainimport (" FMT ") func main () {i: = ten var J float64 = Float64 (i)//without an explicit conversion, the statement will error FMT. Println ("J", J)} "[Online Run Program] (https://play.golang.org/p/Y2uSYYr46c) on line 9th, I is converted to the float64 type, followed by the assignment to J. If you do not perform a type conversion, the compiler throws an error when you try to assign I to J. This tutorial ends here. Please leave your feedback and questions in the comments section. * * Previous tutorial-[variables] (https://studygolang.com/articles/11756) * * * Next tutorial-[constant] (https://studygolang.com/articles/11872) * *

Via:https://golangbot.com/types

Author: Nick Coghlan Translator: Noluye proofreading: polaris1119

This article by GCTT original compilation, go language Chinese network honor launches

This article was originally translated by GCTT and the Go Language Chinese network. Also want to join the ranks of translators, for open source to do some of their own contribution? Welcome to join Gctt!
Translation work and translations are published only for the purpose of learning and communication, translation work in accordance with the provisions of the CC-BY-NC-SA agreement, if our work has violated your interests, please contact us promptly.
Welcome to the CC-BY-NC-SA agreement, please mark and keep the original/translation link and author/translator information in the text.
The article only represents the author's knowledge and views, if there are different points of view, please line up downstairs to spit groove

4,514 Reads

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.