[Go language] basic data type

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. 1 Basic data types
There are several variable types of go:
1) Boolean type (Bool:1 bit)
Boolean type only true and false two values
2) integral type (int)
BYTE type: Byte (equivalent to Int8:1 bit)
Loadline type: Rune (equivalent to Int32:4 bit)
unsigned: int unit (unsigned + prefix u)
According to the number of digits: Int8 int16 int32 (Int64
3) float type (float)
According to the number of digits: float32 float64
4) String type (strings)
A string variable that is caused by using double quotation marks "" or "anti-quotation marks", using UTF-8 encoding.
5) plural type (complex)
Based on the number of digits complex64 complex128
6) fault type (Error)

Description
A. Different types, different lengths are not allowed to convert each other, can not be assigned to each other.
B. String type variable values are non-modifiable. You cannot modify the characters inside.
C. Depending on the platform, the 32-bit system "int" and "Int32" are of the same type, but Byte and int8 are the same type



2 Defining variables
1) Defining variables
If a variable is defined and is not assigned, the initial value is automatically assigned at compile time.
var variable name variable type
Like what:
var x int
var x, y int


2) Define variables and assign initial values
var variable name Variable type = variable value
var variable name = variable Value
Variable name: = Variable Value
Like what:
var x int = 1
var x, y =
x,y:=1,2

3) Multi-variable declaration or definition
var x, y, Z int Declaring the same type variable
var Declaring types with different variables
x int
Y float
z bool
)
var x, y = 1,1.3 Assigning values to variables
X, y: = 1,1.3 Assigning values to variables


3) Initialization of variables:
When you use Var to define a variable, it is automatically initialized to a value of 0.
Bool:false
Integral type: 0
Floating-point type: 0.0
String type: ""
pointers, functions, interfaces, Sclice,channel,map:nil

4) Other points of attention.
The A.go compiler has an error in declaring a variable that is not used. You can use _ variables instead of
B._ is a special variable name, and any value assigned to it will be discarded.

3 Constants
1) declaring constants with const, declaring that they can be omitted type
Const constant Name Variable type = variable value
Const constant NAME = variable value (omitted type)
For example
const x INT = 2
Const Y = 2
2) enumeration value
Description: Can use Itoa to represent, each use from 0 plus 1. For example
Const
x = Iota
y = Iota
z = Iota
)
The x, y, and Z are 0,1,2 respectively. Must be placed in () before it is incremented.
3) grouping. Multiple definitions can use grouping, as follows:
VAR (
XM = 1
y = 2
)
Const
XM = 1
y = 2
)
Description
Constants do not support suffixes. such as L,F, etc.


4 Type conversions
1) Implicit conversions are not supported. The go language does not make any implicit conversions to different types of variables.
2) Only a few types of casts are supported.
allowable conversions
1) string to Byte, string to int slice
[]byte (str) can convert a string type to a byte type
[]int (str) can convert a string type to an int slice type

2) bytes converted to string
String (b) You can convert a byte type to a string type

3) int Slice to string
String (Intslice) to convert int slice type to string type

4) Integer Reciprocal transfer
UNIT8 (INTX)

5) integer, floating-point mutual transfer
Int (FLOATX)//truncates the small number part
Float (INTX)

6) Custom Switch to
If 2 types have the same field. Attempts can be converted to each other.

Integer number polygon
Decimal digit polygon = {decimal number}.
octal digit polygon = "0" + {octal number}.
hexadecimal digit polygon = "0" + ("x" | "X") + {hexadecimal number}.

Floating point literal = decimal number "." [Decimal number] [Index E] |
Decimal Index E |
"." Decimal number [exponent e].
decimal number = decimal number {decimal number}.
Index E = ("E" | "E") ["+" | "-"] decimal number.
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.