Strconv package example

Package main import (" FMT ""strconv") func main() { String convert */ //1.bool Type S1 :="true" B1,err:=strconv.ParseBool(s1) if err! =nil { fmt.Println(err) return } fmt.Printf("%T,%t\n",b1,b1) ss1:=strconv.FormatBool(b1) fmt.Printf("%T,%s\n",ss1,ss1) / / 2. Integer s2:="100" i2,err:=strconv.ParseInt(s2,2,64) if err! Println(err) return} fmt.Printf("%T,%d\n",i2,i2) ss2:=strconv.FormatInt(i2,10) fmt.Printf("%T,%s\n",ss2,ss2)  //itoa(),atoi() i3,err:=strconv.Atoi("-42") fmt.Printf("%T,%d\n",i3,i3) ss3:=strconv.Itoa(-42) fmt.Printf("%T,%s\n",ss3,ss3) }Copy the code

The function first

Package main import "FMT" func main() {/* Function: function Can avoid duplicate code 2. Enhance program extensibility 3. Use: step1: define Step2: call 4. Func funcName()(){// Return val1,val2} 2. Function name (actual arguments) */ /* 1. Define package 2. */ getSum()} getSum() {sum:=0 for I :=1; i<=10; i++ { sum+=i } fmt.Printf("%d",sum) }Copy the code

The execution of a function

Function considerations:

  1. Define before you use
  2. If a function is defined and not called, it is meaningless
  3. Function names must not conflict
  4. Main () is a special function that the system calls automatically

Function arguments

Real participation parameter

        getSum(5)
	i:=4
	getSum(i)
Copy the code
func getSum(n int) { sum:=0 for i:=1; i<=n; i++ { sum+=i } fmt.Printf("%d\n",sum) }Copy the code