This is a creation in Article, where the information may have evolved or changed.
- Each go source file starts with the package declaration, which indicates which packages the go code belongs to
- To generate an executable program for go, you need to define a main package and create a main directory
- External parameters are passed in with OS module OS. The args method gets, os. args receives a parameter subscript starting from 1
Package Mainimport ("FMT" "OS") Func main () {FMT. Println ("Hello Test") fmt. Println (OS. ARGS[1])}
- Return values that are not explicitly assigned when the function returns are set to the default values, such as the float type return value is set to 0.0,error type return value will be set to nil
- 6g and 6l are 64 versions of the go compiler and linker, 32 for versions of compilers and connectors that are 8g and 8L,GCC versions of compilers are GCCGO
6g helloworld.go6l helloworld.6./6.out output: Hello test test OS. Args
- Xxx_test.go is the naming convention for unit tests in the go language, and the GOPATH,GOROOT environment variable is set to run cmd
Goroot:d:\gogopath:d:\workspace01\gopractisego Test Simplemath
- Const is used to define constants, and iota is a special keyword that automatically adds 1 when the next constant appears, and resets to 0 the next time a constant occurs.
- Constant enumeration type definition, lowercase letters are visible inside the package
Const (Sunday = iotamondaytuesdaywednesdaythursdayfridaysaturdaynumberofdays)
- Types in the Go language
Boolean type: bool integer: int8 byte (uint8) int16 int uint UIntPtr floating-point type: float32 float64 Complex Type: complex64 complex128 string: String character type: Rune FMT. Println ([]rune ("Hello")) output is an array of characters converted to Unicode error type: Error pointer array slice dictionary channel struct interface
- There is no automatic type conversion between int and Int32, which is defined by the go language as two types, exception occurs unless the problem is resolved with coercion type conversion
- You cannot use = = to determine whether the floating-point number is equal, which can affect the stability of the result, the following method is recommended
Import "math" //p for user-defined comparison accuracy, such as 0.00001 func isequal (F1, F2, p float64) bool {
- The go language currently supports only UTF8 and Unicode encoding formats of two
- Map usage Examples
Package Mainimport ("FMT") type PersonInfo struct {Name stringage intsex bool}func mapdecalre () {var persondb map[string] P Ersoninfopersondb = Make (map[string] PersonInfo) persondb["1"] = personinfo{"Lucy", 20,true}persondb["2"] = personinfo{ "Lily", 21,true}persondb["3"] = personinfo{"Lilei", 22,false}person,ok: = persondb["1"]if OK {fmt. Println (person. Name = = "Lucy") fmt. Println ("OK")} else {fmt. Println ("Fail")}}func Main () {Mapdecalre ()}
- The statement body of a conditional statement must be enclosed in curly braces and not allow the final return statement to be placed in the conditional judgment or compile an error
func example (x int) int { if x = = 0 { return 5 } else { return x }} Compile Error
- A conditional statement after a switch in a switch...case statement may not be required
Switch {case 0 <= num && num <= 3: FMT. Printf ("0-3") case 4 <= num && num <= 6: FMT. Printf ("4-6") Case 7 <= num && num <= 9:
- Use Fallthrough in Switch...case to execute a case statement immediately following
Switch I {case 0: fmt. Printf ("0") Case 1: FMT. Printf ("1") Case 2: fallthrough case 3: FMT. Printf ("3") Case 4, 5, 6: FMT. Printf ("4, 5, 6") default: FMT. Printf ("Default")} runs the above case and will get the following result: i = 0 o'clock, output 0, i = 1 o'clock, Output 1, i = 2 o'clock, output 3, i = 3 o'clock, output 3, i = 4 o'clock, output 4, 5, 6; i = 5 o'clock, output 4, 5, 6; i = 6 o'clock, output 4, 5, 6; i = Any other value, output default.
- Switch supports type determination at the same time
Func myprintf (args ... interface{}) {for _, arg: = range args {switch arg. (type) {case int:fmt. Println (ARG, "is an int value.") Case String:fmt. Println (ARG, "is a string value.") Case Int64:fmt. Println (ARG, "is an Int64 value.") Default:fmt. Println (ARG, "is an unknown type.")}}
- Goto and Break statements
Func Gotofunc () {i: = 0here:fmt. Println ("here") i + + if I < {goto here}}func Breakfunc () {jloop:for J: = 0; J < 5; J + + {for I: = 0; i < 10; i + + {if I > 5 {break jloop}fmt. Println (i)}}} Execution results HEREHEREHEREHEREHEREHEREHEREHEREHEREHERE012345
- Functions that begin with lowercase letters are visible only within this package, and functions that begin with uppercase letters can be used by other packages. This rule also applies to the visibility of types and variables.
- Indefinite parameter passing
Func myfunc (args ... int) { //pass MYFUNC3 as-is (args ...) Passing fragments, virtually any int slice can be passed in.
- Any type parameter pass can use interface{}
Func Printf (format string, args ... interface{}) {
- anonymous function calls
Func (i int) {FMT. Println (i)} (1)
- anonymous function calls
var J int = 5a: = Func () (func ()) {var i int = 10fmt. Println (i) return func () {fmt. Println (J)}} () //Look at how to remove brackets a () do not output brackets 10 5 Remove parentheses output 10 Remove parentheses This is because only the return value is returned and no return value is executed.
- There can be more than one defer statement in a function, so it is important to note that the invocation of the defer statement is in accordance with the advanced post-out principle that the last defer statement will be executed first
- The flag pack is a package that the Go standard library uses to provide fast parse command-line arguments
Package Mainimport ("Flag" "FMT") var infile *string = flag. String ("I", "infile", "File contains values for sorting") var outfile *string = flag. String ("O", "outfile", "File to receive sorted values") var algorithm *string = flag. String ("A", "Qsort", "Sort algorithm") func main () {flag. Parse () if infile! = nil {fmt. Println ("infile =", *infile, "outfile =", *outfile, "algorithm =", *algorithm)}} command-line arguments:-I unsorted.dat-o sorted.dat-a bub Blesort Run Result: infile = unsorted.dat outfile = Sorted.dat algorithm = Bubblesort if removed: flag. Parse () Run result: infile = infile outfile = outfile algorithm = qsort
- Fmt. Println (") for multi-line output