This is a creation in Article, where the information may have evolved or changed.
Go like PHP, the end of each line is added to the knot, the difference is that go is not compelling, this is like JavaScript, and the language of Go is wrapped in a pair of large parentheses, but go requires that the left large parenthesis must be at the end of the language, not at the beginning of the left large parentheses, This is a new feature, at least for the first time in the language that I'm touching. And it's the go-to-size-write-sensitive, which I'm sure you won't have any arguments.
The "Go" method is the same as PHP, the single line of the statement//, and the multi-line interpretation of * * to the content * *, this is not the difference.
Go has a known type of int, this type is very special, he will self-based on your hardware to determine the appropriate length, on the 32-bit hardware, his length is 32, on 64-bit hardware, his length is 64 bits, of course, you can also make sense of the length of a variable, such as Int8,int16 , Int32,int64 and Byte,uint8,uint16,uint32,uint64. Note that the byte here is uint8.
Floating point type float32 and float64
The type of the model is determined by the constants true and false, meaning that the method is: var x bool=true;
Go native support complex, variable type is complex128;complex64;complex32
There are also a few special types, which are error and uintptr, which are then discussed later
Remember tens do not mix these types, mixing will cause the wrong, look at the next generation code:
Package Main func main () { var a int var b int32 a = 5 B = A + a B = b + }
This code has been mixed with the digital type, so it will be wrong when you edit it, and if you want to make sure that it's not wrong, you have to use the explicit conversion to do it, and then we'll talk about this part.
There are many ways to define go language, and the first way is to generalize that there are only two kinds of words:
var variable name type = initial value
For example:
var a int = 123
You can also omit the type
var a=123
This approach is primarily used to define global variables, which can actually extend many different ways of writing, such as
var ( a int b bool c,d string = "abc", "FF" )
For these methods of writing, as long as one can be used, the other different ways of writing as long as they can recognize what he is doing is enough, there is no need to remember every hard back.
On the top, there is the first definition, and there is another way to define it in a more simplified way, but this definition can only be used in the inside of the function, and its notation is as follows:
A: = 123
A, B: = 20, 16
_, B: = 34, 35
There is a very special change in go language, that is the system preset _, all the values given to this variable will be self-discarded, this is because go language forbid the variable is not used, once encountered this definition of the changes can not be used in the situation, it is possible to give value to _.
From the previous lesson we learned that the import package would be wrong if not used. In this lesson, we also learned that the variables of the definition would be wrong if it didn't work, and I think it was great to see the intentions of the go language. It's compelling to me that the program is required to write strict kan procedures.
There are so many things to be sure of, just a little bit, that is, if you want to define the amount of variables that can only be determined by Var, not by: = This form of contraction, because: = can only use the function inside.
Constant definition and variability are almost no different, just to change the key to a const strikes, and another one of the type iota look at the next example:
Const (
A = Iota
b = Iota
)
The value of a is 0 when I first gave a value, and the second time I give B copy, the value of B is 1.
Here I am particularly clear about the string type, which is the type of go language, but he is very different from PHP, because in go language once the string is not changed after the value.
See Example:
var s string = "Hello"
S[0] = ' C '
This is not allowed, in addition, the string type must be used with the double-cited, not the single-argument, because it represents a character rather than a string, but you can give the string with a backslash, that is, we write the MySQL language in the time of the anti-quote, such as SELECT * from ' User ' WHERE ...
So if I want to change the string, look at the next code:
To implement this in Go, you need the following method:
s: = "Hello" //variable s value, type is string c: = []rune (s) //string s is converted into a character type, Rune is Int32 's name c[0] = ' C ' // Modify the value of the first element in the group s2: = string (c) //Convert the variable C of the number to a string and re-value the variable FMT to S2 . Printf ("%s\n", S2) //Output
It's a little troublesome, at least more troublesome than PHP.
S: = "This is a" + "string thread" s: = ' It can actually be written like this , but it will be more of a newline character to write
In addition to the notes above, the go has some reserved words that are not available, as follows:
Break default Func Interface Select
Case defer go map struct
Chan Else goto package Switch
Const Fallthrough If range type
For import return var continue
Through this article it is not difficult to discover that the type of go is actually not much, but the function is not weak, because go can be self-defining type, here is a note, in the subsequent article will be detailed. In addition, the way to define the variable or constant is more spiritual, there are many ways to write, the constant time to pay attention to the type, The time to define the variable is to note that the string is a very special type, and that's all it takes.
Hope to download the article friends can leave an article out, http://see7di.cnblogs.com here grateful!
Just set up a QQ group, interesting talk about go language friends can join: 195112