This is a creation in Article, where the information may have evolved or changed.
Defining variables
Using the var keyword is the most basic way to define variables in the go language, and the go language does not work with the C language to put variables behind variable names.
Defines a "liuxinming" variable with a variable of type "var liuxinming type"
Define multiple "type" variable var name1,name2,name3 type
The variable that initializes the liuxinming is "value", and the type is "type" var liuxinming type = value
Define three variables of type "type" and initialize var name1,name2,name3 type = three-in-one
Simplify the above statement name1,name2,name3: =
Description:
": =" This symbol directly replaces the Var and type, which is called a short declaration, but it has a limit that can only be used inside the function (it will not compile if used outside the function). So we generally use VAR to define global variables. "_" is a special variable name, and any value given to it will be discarded.
_,b = 1,2//b is given 2, and 1 is discarded at the same time
Go appointment An error will be made during the compile phase for declared but unused variables, as follows
Package MainFunc Main () { var i int//declared I, but not used}
Constant
The value is determined at the time of program compilation, and the program cannot change the value at runtime, and in the go language, constants can be defined as numbers, Boolean values, or string types
Const CONSTANTNAME = value//You can explicitly specify a constant type const Pi float32 = 3.1415926const i = 10000const maxthread = 10const prefix = "if required Astaxie_ "
Boolean type
Boolean value of type bool, with a value of true or FALSE, false by default.
Instance code VAR isactvie bool//global variable declaration var enabled,disabled = true, false//ignore type declaration func test () { var available bool//General Declaration
valid: = false//short declaration available = TRUE//Assignment action}
Numeric type
Integer type:
1, with 2, unsigned go language supports both int and uint, both types are the same length, but the exact length depends on the implementation of different compilers. Go reservations also have a direct definition of the type of the number of digits:
Rune (Int32 's nickname)Int8int16int32int64byte (Uint8 's nickname)Uint8unit16uint32uint64 Note: These types of variables are not allowed to be assigned to each other or operate
Error code var a int8var b int32c: = a + b
Types of floating-point numbers:
float32 float64 (default) The Go language also supports complex numbers, which default type is complex128 (64-bit real number + 64-bit imaginary number) comple The x64 complex is in the form of Re+imi, where re is the real part, IM is the imaginary part, and the last I is the imaginary unit.
Package Mainimport "FMT" Func Main () {var c complex64 = 5 + 5ifmt. Printf ("Value is:%v", c)}
String
The go language strings are encoded using the UTF-8 character set. The string is defined in a pair of double or inverted quotation marks (' '), and its type is string.
Example code func Test () { userame,realname: = "grassroots", "liuxinming"//Short declaration username2: = "Test" username2 = " Test2 "//Regular assignment}
in the Go language, the string is immutable, and the following code compiles with an error
var s string = "Hello" s[0] = ' C '
You can use the following code to implement
/* String Sample code * @author LXM */package mainimport "FMT" Func main () {s: = "Hello" c: = []byte (s) //convert string s to []byte type S2: = String (c)//converted back to the string type FMT. Printf ("%s\n", S2)}
The "+" operator can be used to link two strings in the go language. Modify the string to refer to the following code:
/* Modify String * @author lxm */package mainimport "FMT" Func main () {s: = "Hello" s = "C" + s[1:]//String slice operation FMT. Printf ("%s\n", s)}
Type of error
The error type, used to handle errors, and the Go language package contains a special packet errors to handle the error.