当前位置:网站首页>Go language learning notes 1.2- variables

Go language learning notes 1.2- variables

2022-06-26 06:23:00 Who made the promise

One 、Go Language variables

  1. Declare variables separately
package main
import "fmt"
func main(){
    
    // The first one is 
	var a int // Declare variable type   In assignment ; No assignment a Default equal to 0
	a=100  
	fmt.Println("a=",a)// Inside , Be similar to PHP Inside .JAVA Inside +
	// The second kind 
	var num=100 // Automatically declare variable types based on values ( Type derivation )
	fmt.Println("num=",num)
	// The third kind of 
	str :="100"   // Omit var【 Cannot be a declared variable , Otherwise, there will be compilation errors 】
 	fmt.Println("str=",str )
}
  1. Declare multiple variables at once
   // The first one is   Declare multiple variables at once 
   var num1,num2,num3 int 
    fmt.Println("num1=",num1,"num2=",num2",num3=",num3)
   // The second kind   Assignment form 
   var i1,i2,i3=100,"100",200
    fmt.Println("i1=",i1,"i2=",i2",i3=",i3)
    // The third kind of   Omit var
   n1,n2,n3 := 100, 20,'name'
    fmt.Println("n1=",n1,"n2=",n2",n3=",n3)

3. Global variables

package main
import "fmt"
var n1 int 
var n2 int 
var n3 int 
func main(){
    
    fmt.Println("n1=",n1,"n2=",n2",n3=",n3)
}

4. Variable use considerations

package main
import "fmt"
func main(){
    
	var i int =10
	// Variables cannot change the data type 
	i=1.2 // An inconsistent data type error will be thrown 
	// Variables cannot be declared repeatedly 
	var i string  // Will throw variables that have been defined , Cannot be defined repeatedly  
}
原网站

版权声明
本文为[Who made the promise]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260613193332.html