Go-built-in base type

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. The Go language includes the following built-in base types:
Boolean type: BOOL
Integral type: int int64 int32 int16 int8 uint8 (byte) uint16 uint32 UInt64 UINT
Float type: float32 float64
Plural type: complex64 complex128
Strings: String
Character Type: Rune
Wrong type: Error

1. Type bool
Keyword: bool
can be defined as: TRUE or false or logical expression
var bool1 bool = Truevar bool2 bool = (2 = = 2)
Note: Other types of assignments cannot be accepted, including (0, 1), or automatic or forced type conversions

2. Integral type
It is divided into two types, signed and unsigned.
Value ranges, such as: int8 2^8-128~127, Uint8 0~255, others and so on
Int/uint its value range is related to the platform, so int32! = int
Support forced type conversion, attention to accuracy issues
Variable 2 = type (variable 1)

3. Floating-point type
Data that contains the decimal point
There are two kinds: float32 float64 (default)
can be converted to each other,
Note: It is better to determine the comparison precision before comparing the size

4. String
How to declare: var str string
Values with "" or "include," can contain multiple lines of string
The operation of a string is the same as an array
Unlike other languages, such as PHP, the contents of a string cannot be modified after initialization, but can be completely re-assigned
S: = "123" s[1] = "3"//compile error
$s = "123"; $s [1] = "3"; Echo $s; 133

5. Character Type
Two types of characters:
byte corresponds to Utf-8
Rune corresponds to Unicode

6. Error type
In the go language, error-handling is the wrong interface, or you can use the errors package
var e error = errors. New ("...")
In the Go language, functions support multiple return values
Errors can be handled in the following ways
Res, err: = Funname (param) If err! = Nil {    //handling Error} else {    //No error}func funname (param) (res type, err error) {    if ... {        err = errors. New (...)        Return    }    ...    return res, nil}

7. Plural

Omitted here, it is necessary to understand


Note_type_1.go code List

Package Mainimport "FMT" Import "errors"//introduced errors bundle//Declaration BOOL Global VAR (enable = Truedisable = False) Func main () {//predefined constant Iotaconst (C0 = Iota//0c1 = Iota//0+1c2//will automatically assign a value or expression that defines a constant) const (C3 = Iota//0c4) fmt. Println ("C0, c1, c2 =", C0, C1, C2) fmt. Println ("C3, C4 =", C3, C4)/*//This compilation will error, need if condition {in the same line of code//missing condition in if statement//enable = = True N OT usedif Enable = = True {fmt. PRINTLN ("error")}*/if enable = = True {fmt. Println ("enabled =", enable)} else {fmt. Println ("enabled =", disable)}/*//compile with the following error://cannot use 1 (type int) as type bool in assignmentenable = 1;//cannot co Nvert 1 to type bool//cannot convert 1 (type int) to type bool//cannot use 1 (type int) as type bool in Assignmentenab Le = bool (1) */var (a int8 = 1b int = 2)//invalid operation:a + B (mismatched types int8 and int)//c: = a + b//need to do type conversion C: = Int (a) + bfmt. Println ("C = a + b =", c)//int32 and int are two different types, but can be coerced type conversion var d int32//d = b//cannot use B (type int) as type int32 IN Assignmentd = int32 (b) fmt. Println ("D =", D) var f1 float32 = 1.23456fmt. Printf ("f1 =%.3f \ n", F1)//1.235f2: = 1.111//compile error:invalid operation:f1 + f2 (mismatched types float32 and Flo AT64//F3: = f1 + f2b1: = (float64 (f1) = = F2)//The comparison method is not rigorous if B1 {fmt. Println ("float64 (f1) = = F2")} else {fmt. Println ("float64 (f1)! = F2")}//uses "" to denote the operation of a string//string as the array var str string = "Hello" fmt. Println ("str =", str) FMT. Println ("str[1] =", str[1]) fmt. Printf ("%c \ n", str[1])//s[i] Take the first i+1 characters//str = "HI"//compile ok//str[0] = ' C '//compile error:cannot assign to str[0]/ /multiline string, with ' ' contains str2: = ' SELECT username, pwd from tb_user WHERE id = 123456 ' fmt. Println (str2) Str3: = "world!" Fmt. Println ("str + STR3 =", str + STR3)//s1 + s2, connection string//len (s) returns the length of the string fmt. Printf ("Length of str2 =%d \ n", Len (STR2))//s[m:n] Returns the string from the start of the M bit to the end of N, M, n can be omitted, at this time m is 0, N is Len (s) s: = "Hello" s = "C" + s[1:] Fmt. Println (s)//cellofmt. Println (S[:3])//celfmt. Println (S[1:3])//elfmt. Println (s[:])//cello//byte with ' ' Include character//var chbyte = "1"//compile error:cannot use "1" (type string) as type byte in Assignmentvar ch1 byte = ' a ' FMT. Printf ("ch1 =%c \ n", ch1)//ch1 = afmt. Println (CH1)//97//runevar CH2 rune = ' B ' fmt. Printf ("CH2 =%c \ n", CH2)//ch2 = bfmt. Println (CH2)//98//errorerr: = errors. New ("Error1") if err! = Nil {//error handling ... fmt. PRINTLN (ERR)}var e1 Errorif e1 = nil {fmt. PRINTLN ("No Problem")}res, E: = div (5, 0)//res, E: = div (5, 1) if E! = Nil {//error handling FMT. PRINTLN (e)//if Div (5, 0) outputs: error:division by zero} else {//correct FMT. Println ("res =", res)//If Div (5, 1), outputs: res = 5}}//defines the function func div (A, b int) (res int, e error) {if b = = 0 {e = errors. New ("Error:division by Zero") Return}return A/b, nil}

Operation Result:



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.