ArticleDirectory
- Condition Statement
- For statement
- Break and continue
- Switch statement
- ++ And -- Statements
- Defer statement
The original English text is here www. Nada. kth. se /~ Snilsson/go_for_java_programmers
Synchronize to http://blog.csdn.net/kkkloveyou/article/details/8308534
Http://bbs.gocn.im/thread-73-1-1.html
=============================== Connect to the text, the following body is =================================.
Condition Statement
Go does not use parentheses in condition statements, suchIfCondition Statement,ForConditional statement expression,SwitchValue of the Condition Statement. On the other hand, it does not needIfOr
ForBrackets in the Condition Statement
If a <B {f ()} If (A <B) {f ()} // parentheses are unnecessary. if (A <B) f () // invalid for I = 0; I <10; I ++ {} for (I = 0; I <10; I ++) {} // invalid
In addition,IfAndSwitchWhen receiving an optional initialization status, the usual practice is to create a local variable.
If err: = file. chmod (0664); Err! = Nil {log. Print (ERR) return err}
For statement
Go does notWhileAndDo-whileStatement. WhenForWhen the condition of a statement is relatively simple, its function is likeWhileStatement. If the condition is completely omitted, an endless loop is generated.
ForThe statement may containRangeTraverse strings, arrays, slices, maps, or channels. In addition to the following:
For I: = 0; I <Len (a); I ++ {...}
TraverseACan also be written
For I, V: = range {...}
HereIIndex,VArray, slice, or string continuous element. For strings, I is a byte index,VPointRuneType (RuneType isInt32). Maps iterations generate key-value pairs, while channels generates only one iteration value.
Break and continue
Like java, go permits break and continue to specify a tag, but the tag must beFor,Switch, OrSelectStatement.
Switch statement
InSwitchStatement,CaseLabels do not pass by default, but you can make themFallthroughThe statement is passed when it ends.
Switch n {Case 0: // empty case bodycase 1: F () // F is not called when n = 0 .}
HoweverCaseIt can contain values.
Switch n {Case 0, 1: F () // F is called if n = 0 | n = 1 .}
CaseCan support any type of equal comparison operators, such as strings or pointers. A switch statement with a lost expression is equivalentTrue.
Switch {case n <0: F1 () case n = 0: F2 () Default: F3 ()}
++ And -- Statements
++And--It can only be used as a suffix operator, and only in the statement, not in the expression. For example, you cannot writeN = I ++.
Defer statement
DeferThe execution of a function called by a statement is postponed until the moment the function returns.DeferWhen a statement is executed, the parameters of the deferred function are calculated and saved for future use.
F, err: = OS. Open ("FILENAME") Defer F. Close () // F will be closed when this function returns.
Constants (constant)
The constant in go may beUntyped. This applies to numeric text without a type constant expression, and the useConstDeclared non-type constant expression. When it is used in the context that requires a value with a type, a constant without a type can be converted into a value with a type. Such constants are relatively free to use, even if go has no 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 does not limit the size of untyped numeric constants. The restriction is only applicable when a constant is used, and one of the types is required.
Const huge = 1 <100var n Int = huge> 98
If it is a non-existent variable declared type and the calculation result of the corresponding expression is an untyped numeric constant, this constant is convertedRune,Int,Float64, OrComplex128Type, 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 have an enumeration type. Instead, you can use special names.IotaIn a singleConstDeclaration to get a series of accumulated values. When the initialization expression is omitted asConstIt reuse the previous expression.
Const (Red = iota // Red = 0 Blue // Blue = 1 green // Green = 2)
Structs (struct)
Struct corresponds to classes in Java, but a member of a structure cannot be a method, but a variable. The pointer of the struct is a reference variable similar to that of Java. Unlike Java classes, the structure can also be defined as a direct value. In both cases.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, methods can be associated with any named type, not just struct. For details, see methods and interfaces.
Pointers (pointer)
If you have an int, struct, or array, You need to allocate an object to copy the content. To achieve the effect of referencing variables in Java, go uses pointers. For any type of ET, There is a corresponding pointer type* TIndicates the pointer type.TValue
Allocate storage space to pointer variables and use built-in functionsNew, Input a type, and return a pointer pointing to the allocated bucket. The allocated space is not initialized. For example,New (INT)Allocate and store it as a new int and initialize it with the value e0And return its address, Type
* Int.
JavaCodeT p = new T (), WhereTIs one or twoIntInstance variablesAAndBCorresponding
Type T struct {a, B INT} var p * t = new (t)
Or do this habitually.
P: = new (t)
VaR V TDeclares a variable that contains a value type.T, Which is not in Java. You can also create and initialize values in composite mode.
V: = t {1, 2}
Equivalent
VaR v TV. A = 1v. B = 2
ForTOperationsX,Address Operator & XThe value type is* TOfXAddress,
============================== Not complete To be continued...
=
2012-12-17