当前位置:网站首页>2-GO variable operation

2-GO variable operation

2022-04-23 14:40:00 Endless character

One 、 Introduction of variables

  • What is a variable : Variables are used to describe the data storage space in the computer
  • Role of variables : Is to save data in computer memory
  • Declaration of variables var Variable name type
package main

import "fmt"

func main()  {
    
	var age int
	var num, sum int
	fmt.Println(age)
	fmt.Println(num)
	fmt.Println(sum)
}
  • Variable initialization : You can assign values to variables when you define them , This process initializes variables
package main

import "fmt"

func main()  {
    
  var age int = 10
  var num int = 20
  fmt.Println(age, num)
}
  • Variable assignment : After the variable is defined , And assign values to variables , That is, declaration before assignment
func main()  {
    
	var a int = 10
	var b int
	b = a
	fmt.Println(b)
}
  • Case study : In exchange for 2 Values of variables
func main()  {
    
	var num1 int = 10
	var num2 int = 20
	var temp int
	temp = num1
	num1 = num2
	num2 = temp
	fmt.Println(num1, num2)
}
  • Automatic derivation type : The so-called automatic derivation type , Just don't go through var Declare variables , Don't specify type , Directly follow the variable name with ”:” Number , Complete the assignment at the same time . that GO The type of variable will be automatically deduced according to the assigned value . If num The variable is assigned a decimal number , Then the type of the variable is decimal ( floating-point )
func main()  {
    
	num := 10
	num1 := 20
	a, b, c :=12, 13, 14
	fmt.Println(num, num1)
	fmt.Println(a, b, c)
}
  • Case study : In exchange for 2 Values of variables ( Realization 2)
func main()  {
    
	num1 := 10
	num2 := 20
	num1, num2 = num2,num1
	fmt.Println(num1,num2)

}

Two 、 Input and output format control

  • Println: Formatted output
  • Printf: Format output ,%d Indicates that the output is the value in an integer variable ,\n Means line break
func main()  {
    
	num1 := 10
	num2 := 20
	num3 := 30
	fmt.Println(num1, num2, num3)
	fmt.Println("num1=", num1)
	fmt.Printf("num1 = %d\n", num1)
	fmt.Printf("num2 = %d,num3 = %d",num2, num3)
}
  • Scanf:Scanf() Grammar format :fmt.Scanf(“%d”,&num)
  • Scan:Scan() Grammar format : fmt.Scan(&num)
  • Variable address : In memory, corresponding storage units will be opened up for variables , In order to be able to find the storage unit, access data , The system will add a number to each unit , This number is the address
  • Scan and Scanf The difference between :Scan You do not need to specify a formatter , later stage Scan More frequently used
package main

import "fmt"

func main()  {
    
	var age int
	fmt.Println(" Please enter age :")
	/* fmt.Scanf("%d",&age) //  adopt Scanf The function assigns the data entered by the keyboard to the variable , But variables must be preceded by & fmt.Println("age = ",age) fmt.Println(&age) fmt.Printf("%p",&age) */
	fmt.Scan(&age)
	fmt.Println("age = ", age)
}

3、 ... and 、 Computer base system

A simple understanding of , And there are a lot of information on the Internet , Including binary conversion and so on

  • What is base : The method of counting according to the principle of carry is called carry counting system .“ Carry numeration system ” Referred to as “ Number system ” or “ Base number ”
    • The carry of each number system follows a rule , That's it ---- Meet N Into the 1
  • Hexadecimal features
    • Use a fixed set of numbers to represent the size of the value . Such as : The decimal number is 0,1,2,3,4,5,6,7,8,9
    • Uniform rules : Meet N Jin Yi
  • Binary element
    • base : there N It's called cardinality . So-called “ base ” It refers to the number of basic digits allowed in various binary counting systems
    • Position right :215=2*102 + 1*101 + 5*100 , among 102 , 101 ,100 Position right
    • Add by weight : Multiply the numeric character on each bit by the weight it represents

Four 、 Variable naming conventions

  • Naming specification
    • ①. Only numbers , Letter ,_( Underline ) form
    • ②. Cannot start with a number
    • ③. Capital letters and lowercase letters are different :heapSort and Heapsort It's two different names
    • ④. It can't be a keyword
  • Naming rules
    • Hump nomenclature
      • Little hump nomenclature (lower camel case): The first word begins with a lowercase letter ; The first letter of the second word is capitalized , for example : myName、aDog
      • Big hump nomenclature (upper camel case): The first letter of each word is capital , for example :FirstName、 LastName
    • Underline separation : Multiple word names , Write in all lowercase letters , In the middle _ Separate , for example :first_name、user_name

版权声明
本文为[Endless character]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231437070977.html