This is a creation in Article, where the information may have evolved or changed.
The English text is in this www.nada.kth.se/~snilsson/go_for_java_programmers
Sync to http://blog.csdn.net/kkkloveyou/article/details/8308534
Http://bbs.gocn.im/thread-73-1-1.html
==================== Next, the following body ========================.
Conditional statements
Go does not use parentheses in conditional statements, such as conditional statements, expressions for conditional statements if
for
, switch
or values of conditional statements. On the other hand, it does not need to if
for
add curly braces in or conditional statements
If a < b {f ()}if (A < b) {f ()} //brackets are unnecessary. If (a < b) f () //invalid for i = 0; I < i++ {}for (i = 0) ; I < 10; i++) {} //invalid
In addition, if
and switch
receive an optional initialization state, then the usual practice is to build a local variable
If err: = file. Chmod (0664); Err! = Nil { log. Print (ERR) return err}
For statement
Go does not have a statement while
do-while
. When for
the condition of a statement is relatively single, his function is like a while
statement. Completely omitting the condition produces a dead loop.
for
Statements may contain range
traversal strings, arrays, slices, maps, or channels. Except as written below.
For I: = 0; I < Len (a); i++ {...}
a
the elements to be traversed can also be written as
For I, V: = Range A {...}
This refers to the i
index, which v
represents the contiguous element of array, slice, or string. For a string, I is the index of a byte, v
pointing to rune
rune
int32
an alias of type (type). The maps iteration produces a key-value pair, and channels produces only one iteration value.
Break and Continue
Like Java, go permits break and continue to specify a label, but the label must refer to for
, switch
or select
statement.
Switch statement
In the switch
statements, the case
tags do not pass by default, but you can get them to fallthrough
end the statement by passing the case.
Switch N {case 0: //Empty case Bodycase 1: f () //F was not called when n = = 0.}
But one case
can contain over a value
Switch N {case 0, 1: f () //F is called if n = = 0 | | n = = 1.}
case
Values can support any type of equality comparison operator, such as a string or pointer. A switch statement that loses an expression is equivalent to the expression true
.
switch {case N < 0: F1 () case n = = 0: f2 () Default: F3 ()}
+ + and--statements
++
And --
can only be used as postfix operators, and only in statements, not in expressions. For example, you may not write n = i++
.
Defer statements
defer
The execution of the statement call to a function is deferred until the function returns that moment. defer
when the statement executes, the parameters of the deferred function are computed and saved for future use
F, err: = OS. Open ("filename") defer f.close () //F'll is closed when the This function returns.
Constants (constant)
The constants in go may be untyped . This applies to numeric literals of untyped constant expressions, and to const
non-type constant expressions that use declarations. When it is used in a context that requires a value of a band type, an untyped constant can be converted into a typed value. The use of this constant is relatively free, even if go does not have an implicit type conversion
var a uintf (A + 1) //The untyped numeric constant 1 becomes typed as uint.f (A + 1e3) //1E3 is also typed as uint .
The language has no limit on the size of untyped numeric constants. Restrictions apply only when a constant is used, and one of these types is required.
Const HUGE = 1 << 100var n int = huge >> 98
If the type of a variable declaration is not present and the corresponding expression evaluates to an untyped numeric constant, the constant is converted to rune
,, int
float64
, or complex128
type, depending on whether the value is a character, integer, floating point, or complex constant.
c: = ' rune ' //(alias for Int32) N: = 1 + 2 //INTX: = 2.7 //float64z: = 1 + 2i //complex128
GO does not exist with enum type. Instead, you can use a special name iota
in a single const
declaration to get a series of accumulated values. When the initialization expression is omitted as one const
, it reuses the preceding expression.
Const ( red = Iota //Red = = 0 blue //blue = 1 green //green = = 2)
Structs (structural body)
structs correspond to classes in Java, but members of a struct cannot be methods, but variables. The pointer to the struct is a Java-like reference variable. Unlike Java classes, structs can also be defined as direct values. In both cases .
, use to access the members of the struct.
Type mystruct struct { s string n int64}var x mystruct //x is initialized to mystruct{"", 0}.var px *mystruct //PX is initialized to nil.px = new (MyStruct) //PX points to the new struct mystruct{"", 0}.x.s = "Foo" PX.S = "Bar"
In Go, a method can be associated with any named type, not just with a struct. See the discussion of methods and interfaces for details.
Pointers (pointer)
If you have an int or struct or an array that needs to be assigned the content copy of the object. To achieve the effect of a reference variable in Java, Go uses pointers. For any type E T
, there is a corresponding pointer type that *T
represents T
the value of the pointer type
Assign storage space to pointer variables, use built-in functions new
, pass in a type, and return a pointer to the allocated storage space. The allocated space will be initialized by 0 type. For example, the new(int)
assignment is stored as a new int, initializes its value to E 0
, and returns its address, type *int
.
Java code Tp=newT()
, which T
is a two int
-type instance variable a
and a b
class that corresponds to the
Type T struct {A, B int}var p *t = new (t)
Or do it habitually.
P: = new (T)
varvT
Represents a declaration that declares that a variable contains a value type T
, which is not in Java. You can also create and initialize values using a composite method.
V: = t{1, 2}
Equivalent to
var v tv.a = 1V.B = 2
For operands of type T
x
, the address operator &x
provides the address of the value type *T
x
,
=================== to be Continued ... ==========
=================== Reprint Annotated Source =============
2012-12-17