This is a creation in Article, where the information may have evolved or changed.
This note is based on the Gocourse document in the Doc folder in the Go Language installation directory, which is included in the reference Format section of this article.
P14 Source is UTF-8.
P15 literals
C-like but numbers require no signedness or size
Markings (more on this soon)
23
0x0ff
1.234e7
C-like strings, but unicode/utf-8.
Raw strings
'/n/.abc/t/' = =//n//.abc//t//
Go literal (literal constants?) ) preserves the style of Class C, but for numbers there is no need to add a suffix for the callout type, whereas for strings there is a raw string without escaping, note that the raw string is not single-quotes at both ends, but rather the characters below.
P16 Syntax Overview
Basically c-like with reversed types and declarations,
Plus keywords to introduce each type of declaration.
var a int
var b, c *int//note difference from C
var d []int
Type S struct {a, b int}
BASIC control structures is familiar:
If a = = b {return true} else {return false}
For i = 0; I < 10; i++ {...}
Well...... Declaring variables seems to be a big hurdle, not a two-day habit. As for the control structure, save two pairs of brackets, if incorrigible, write of course also can. However, it is important to note that curly braces that can be omitted in most Class C languages when there is only one statement in the execution section are now required.
Numeric types
Numeric types is built in, would be is familiar:
< P>int |
uint |
float |
int8 |
Uint8 = byte |
&NBSP; |
Int16 |
uint16 |
&NBSP; |
< P>int32 |
UInt32 |
float32 |
Int64 |
UInt64 |
float64 |
Also uintptr, an integer big enough to store a pointer.
These is all distinct types; Int. Int32 even on
A 32-bit machine.
This is an unexpected level of granularity, but there is no need to guess the number of integers on the platform, directly using the type of the specified digits. But different types can not be directly compared, so if you write the following code, the compiler will directly error (go compiler default settings appear to be very strict, even if you import a package or define a variable does not use it will be an error)
int32 = 0
2:max: = 1
if i < max {...}
As for int is not int32, it probably means that they belong to different categories of things, but the value range of the two on 32-bit platforms (at least on my PC) is exactly the same (-2147483648 to 2147483647), and on my 64-bit platform, The value range of int is also consistent with int32 (So what is the difference between them?). Will this question be shelved in the basic course?).
The go operator is similar to C, ^ instead of ~ and so on. But it's important to note that
+ + and--is not expression operators
(x + + is a statement, not an expression;
*p++ is (*p) + + not * (p++))
In fact, a statement written almost instinctively may be wrong, such as
< x + + {...}
As for the type conversion between the numbers, may God bless my habits.
In addition, for the accuracy of constant expressions, go processing seems a bit exaggerated
High precision:
Const LN2
= 0.693147180559945309417232121458/
176568075500134360255254120680009
Const LOG2E
= 1/LN2//Accurate reciprocal
Representation is "big enough" (1024x768 now).
About variable declarations, to be honest, I don't understand why there must be a var keyword
Variable declarations is introduced by Var.
They may has a type or an initialization expression;
One or both must be present.
Since one or both must be present, the declarative statement can be inferred by the compiler, that is, the only reason the Var keyword exists is simply to prevent the programmer from defining a new variable that is not meant to be a typo (or to write the statement incorrectly as an assignment). As for why the type is placed behind the variable name, the document does a lot of explaining ... Unfortunately I did not see why: (
But the new short declaration symbol would be popular.
The: = "Short Declaration"
within functions (only), declarations of the form
var v = value
Can is shortened to
V: = value
The go documentation says this is another reason to put the type behind the variable name (anyway, go means the name, but I don't care if you're a shrimp). Note that this symbol supports parallel assignments (the equal sign is also supported).
Of course, another use of the Var keyword is to go with parentheses, once the "wholesale" n variables, which are the same as the Const keyword
Const (
Monday, Tuesday, Wednesday = 1, 2, 3;
Thursday, Friday, Saturday = 4, 5, 6;
)
When you define a const, you can use the Iota counter. Well, now you have the enum.
Constant declarations can use the counter iota,
Which starts at 0 in each const block and increments
At each semicolon.
Shorthand:previous type and expressions repeat.
Const (
LOC0, bit0 UInt32 = Iota, 1< loc1, bit1; 1, 2
LOC2, Bit2; 2, 4
)
The control structure and C are similar. Note that the C-style comma delimiter is not allowed in the For loop, that is, when you need to represent i = 1, j = 2 o'clock should be replaced by a parallel assignment style such as I, j = 1, 2.
Switch's expression is a lot different from C.
But there is important differences:
-Expressions need not being constants or even ints.
- No automatic fall through
-instead, lexically last statement can be Fallthrough
-Multiple cases can be comma-separated
Switch Count%7 {
Case 4,5,6:error ();
Case 3:a *= v; Fallthrough;
Case 2:a *= v; Fallthrough;
Case 1:a *= v; Fallthrough;
Case 0:return A*v;
}
In the above example, if the result of count%7 is 2, then after a *= V of Case 2 is run, a *= 2 is run again and the A*V is returned. I guess, such a grammar ... No one will get into trouble for forgetting the break. The enhanced switch has even become a more elegant alternative to the If-else chain.
The expressions can is any type and a missing
Switch expression means true. Result:if-else chain:
A, B: = X[i], y[j];
Switch {
Case a < b:return–1
Case a = = B:return 0
Case a > B:return 1
}
Or
Switch A, B: = X[i], y[j]; { ... }
Perhaps this is the go language switch real battlefield: Instead of a long list of If-else, perhaps for this reason, the break was forced to exit in the switch, and joined a Fallthrough have not met.
A function can return multiple values. If So, the
Return types is a parenthesized list.
Func mysqrt (f float) (float, bool) {
If f >= 0 {return math. Sqrt (f), true}
return 0, False
}
Func mysqrt (f float) (v float, OK bool) {
If f >= 0 {V,ok = Math. Sqrt (f), true}
else {V,ok = 0,false}
Return V,ok
}
Multi-value return ... Thank Allah for giving me a multivalued return:) However, it can be expected that multivalued returns will have a great impact on how and how programmers handle errors. See. For a function that defines a return type or a return variable, a separate return means that the current value of the returned variable is returned. Unlike other advanced dynamic languages, the return statement must still be written explicitly here.
In function, you can use the Defer keyword
Defer
The defer statement executes a function (or method)
When the enclosing function returns.
Well, I think of the finally statement. But it's not the same. The output of the following results is 1 0 2
Package Main
int) (int) {
4: v++
5: println (v)
6: return
7:}
9:func Main () {
Ten: I: = 0
One: //F (i) evaluated, print 1; function return 2, but deferred
: //0
13:}
...... A turn to the next page to see a better routine, depressed. However, this immediate assessment, delayed return of the syntax with obvious hacker habits, is not likely to create new style confusion is still difficult to say. But to be sure, Joel Spolsky will not like this grammar. He will tell you that there is an invisible goto ... Related things are not concentrated together ... Wait, if you don't believe him, he'll keep telling you that Raymond Chen is standing on his side.
In function, there is something similar to (that is? ) The syntax of the anonymous function:
Func f () {
For I: = 0; I < 10; i++ {
G: = func (i int) {FMT. Printf ("%d", I)};
g (i);
}
}
U GOT IT, we have closures!
For clients (importers) of the package, names must
Be-upper case-to-be visible
!! For me it's too ... It's strange. Use case to distinguish public or private?? and Python spaces have a spell.
2) an init () function, as many as one per source file.
Package dependency guarantees correct execution
Order.
Initialization is always single-threaded.
A bit like the static {} in Java.
When you organize your Go program's source code structure, you can classify each folder as a package, and the source files in each of the packages need to be compiled together (except for the test code).
Testing
To test a package, write a set of Go source files
within the samepackage; Give the files names of the
Form *_test.go.
Within those files, global functions with names
Starting test[^a-z]* 'll be run by the testing tool,
Gotest. Those functions should have signature
Func testxxxx (t *testing. T
the "testing" package provides support for logging,
Error reporting.
The naming convention plays an important role again.
The first lesson ended, the exercise is Fibonacci series, the first time when writing did not do the cache, the result has become the result of the spraying algorithm, immediately add map, the constant speed forward! Gobuild is very useful, do not write makefile can automatically fix together ... In addition to testing, the real use of the only to find that the-t parameter is still "not implemented" status ...
ps:64 bit unsigned integer, really tnnd big AH.
Package Exercise
Import "os"
4:var m map[int]uint64
int) (v UInt64) {
7: If i < 1 {
8: OS. Exit (1)
9: }
10:
One: if m = = Nil {m = make (map[int]uint64)}
: v = m[i]
: ifreturn }
: Switch i {
: Case 1, 2:v = UInt64 (i)
+: default: v = Fibonacci (i-1) + Fibonacci (i-2)
: }
: M[i] = V
: return
21:}
******
Quote of the day:
Read, every day, something no one else is reading. Think, every day, something no one else is thinking. Do, every day, something no one else would is silly enough to do. It is the "bad" for the mind to being always a part of unanimity. -Christopher Morley