The second chapter, Go- basic grammar

2.1 Variable Definition

(1) First, helloworld

package main

import(
	"fmt"
)

func main() {
	fmt.Println("helloworld")
}

The initial value (2) variable

If you define a variable, not assigned, there will be a default initial value

main Package 

Import ( 
	"FMT" 
) 

FUNC variableZeroValue () { 
	// definition of two variables, no assignment 
	var A int 
	var S String 
	fmt.Printf ( "% D% Q \ n-", A, S) // formatted output 
} 

FUNC main () { 
	fmt.Println ( "HelloWorld") 
	variableZeroValue () // A, S initial value: 0 "" 
}

Definition and assignment (3) variables 

main Package 

Import ( 
	"FMT" 
) 

FUNC variableInitialValue () { 
	defined variables and assignment // 
	var A, B int = 3,4- 
	var S String = "Derek" 
	fmt.Println (A, B, S) 
} 

FUNC variableShorter ( ) { 
	// colon 
	D, E, F: =. 5, to false, "Zhang" 
	fmt.Println (D, E, F) 
} 

FUNC main () { 
	variableInitialValue (). 4. 3 // Derek 
	variableShorter () //. 5 Zhang to false 
}

(4) var () variable defined within

main Package 

Import ( 
	"FMT" 
) 

// centrally define variable 
var ( 
	AA = 2 
	BB to true = 
	CC = "Jack" 
) 

FUNC main () { 

	fmt.Println (AA, BB, CC) to true // 2 Jack 
}

 to sum up

  • Var keyword: var s1, s2 string = "zhang", "derek"
  • Let the compiler automatically select Type: var a, b, i, s1, s2 = true, false, 3, "hello", "world"
  • Colon equal to a defined variable: a, b, i, s1, s2: = true, false, 3, "hello", "world", can only be used within a function

2.2. Built-in variable types

(1) Type

  • bool,string
  • (And) you, (and) int8, (and) int16, (and) int32, (and) int64, unitptr
  • bytes, Rune
  • float32,float64,complex64,comlex128

(2) cast

main Package 

Import ( 
	"FMT" 
	"Math" 
) 

FUNC main () { 
	var a, b = int 3,4- 
	// Sqrt must receive float64 type, first a, b and converted into a square float64 type, then the results converted to type int 
	var C = int int (Math.sqrt (float64 (A * A + B * B))) 
	fmt.Println (C). 5 // 
}

2.3. The constants and enumerations

If no value const type can be used as various types you may also specify the type.

Defined in (1) constant

main Package 

Import ( 
	"FMT" 
) 

FUNC main () { 
	// do not specify the type, can be used as various types 
	const A, B = 3,4- 
	// specified as a string type, for the specified type 
	const str string = "Derek" 
	// can not each write in brackets const 
	const ( 
		C = to false 
		D = "Jack" 
	) 
	fmt.Println (A, B, STR). 4. 3 // Derek 
	fmt.Println (C, D) // Jack to false 
}

(2) enumerated type

package main

import(
	"fmt"
)

func main() {
	const (
		java = 0
		python =1
		golang = 2
	)
	fmt.Println(java,python,golang)     //0 1 2
}

(3) the use of value-added iota from the enumeration type

main Package 

Import ( 
	"FMT" 
) 

FUNC main () { 
	const ( 
		Java // IOTA IOTA = const represents the set value from the 
		Python 
		golang 
		JavaScript 
	) 
	fmt.Println (Java, Python, golang, JavaScript) // 2. 3. 1 0 
}

2.4. Conditional statements

(1) if the statement

if the statement is not required conditions inside the brackets

main Package 

Import ( 
	"FMT" 
	"IO / ioutil" 
) 

FUNC main () { 
	const filename = "C: \\ \\ Desktop the Users 86 158 \\ \\ \\ awesomeProject named abc.txt the src \\" 
	//ioutil.ReadFile : returns two values, the contents of a file is read out, an error message is 
	// if's condition can be assigned 
	if contents, ERR: ioutil.ReadFile = (filename); ERR = nil {! 
		fmt.Println (ERR) 
	the else {} 
		fmt.Printf ( "% S \ n-", Contents) 
	} 
	scope // if conditions in the variable assignment in the if statement, the outside like this visit if the error 
	fmt.Println (Contents) 
}

(2) switch Statement

  • After the switch can no expression
  • switch does not need to break
  • A plurality of conditions can be directly switch
package main

import(
	"fmt"
)

func grade(score int) string{
	g := ""
	switch {
	case score < 0 || score > 100:
		//报错信息
		panic(fmt.Sprintf("错误的分数:%d",score))
	case score < 60:
		g = "D"
	case score < 80:
		g = "C"
	case score < 90:
		g = "B"
	case score <= 100:
		g = "A"
 	}
	return g
}

func main() {
	fmt.Println(grade(2))
	fmt.Println(grade(70))
	fmt.Println(grade(83))
	fmt.Println(grade(101))     //panic: 错误的分数:101
}

2.5. Cycle

for

  • there is no need for brackets conditions
  • for conditions where you can omit the initial conditions, the end condition, increment expression
package main

import "fmt"

func main() {
	sum := 0
	for i :=1; i <= 100; i++{
		sum += i
	}
	fmt.Println(sum)
}

2.6. Functions

Features:

  • The return value type written in the last surface
  • You can return multiple values
  • Function can be used as parameters
  • There is no default and optional parameters

(1) Basic usage

main Package 

Import "FMT" 

// the name of the function (function parameters, parameter types) return type 
FUNC the eval (A, B int, String OP) {int 
	Switch OP { 
	Case "+": 
		return A + B 
	Case "-": 
		A return - B 
	Case "*": 
		return A * B 
	Case "/": 
		return A / B 
	default: 
		// error message 
		panic ( "UNSUPPORT Operation:" + OP) 
	} 
} 

FUNC main () { 
	fmt.Println (the eval (3, 5, "+")) // 8 
	fmt.Println (eval (3, 5, "*")) // 15 
	fmt.Println (eval (3, 5, "GG")) // panic: Operation UNSUPPORT: GG 
}

(2) two return values

main Package 

Import "FMT" 

// returns two values 
FUNC div (A, B int) (q, r int) { 
	return A / B, A% B 
} 

() {FUNC main 
	// receiving two values 
	q, r : = div (13,3) 
	fmt.Println (Q, R & lt). 1. 4 // 
}

 Only receives a return value

main Package 

Import "FMT" 

// returns two values 
FUNC div (A, B int) (Q, R & lt int) { 
	return A / B, A% B 
} 

FUNC main () { 
	// if you want to receive a return value can be used "_" 
	Q, _: = div (13,3) 
	fmt.Println (Q) //. 4 
}

Guess you like

Origin www.cnblogs.com/derek1184405959/p/11273565.html