Golang input

Time:2021-11-25

The programs we have written above assign values to variables directly. However, in many cases, we want users to enter a value through the keyboard, then store it in a variable, and then take out the value of the variable for operation

So how to receive the user’s keyboard input in go language? The specific operations are as follows:

The first form: FMT. Scanf ()

package main

import "fmt"

func main() {
	var age int
	FMT. Print ("please enter your age:")
	fmt.Scanf("%d", &age)
	FMT. Printf ("your age is:% d", age)
}

In the go language, we usefmtIn this bagScanf()Function to receive data entered by the user’s keyboard

When the program is executed toScanf()After the function, it will stop executing and wait for the user’s input. After the input, the program will continue to execute

What we should pay attention to here isScanf()Writing format of function:

  1. To use “% d” to indicate that the input is an integer, enter the complete number and store it in the variableageYes
  2. thereageVariables must be preceded by&Symbol to obtain the address of a memory unit before it can be stored

The second form: FMT. Scan ()

There is another way to obtain user input data, as follows:

package main

import "fmt"

func main() {
	var age int
	FMT. Print ("please enter your age:")
	fmt.Scan(&age)
	FMT. Printf ("your age is:% d", age)
}

adoptScan()Function receives user input and can be omitted%d, easier to write

Li peiguan blog

lpgit.com

Recommended Today

Vue、Three. JS implementation panorama

1、 First, we need to create a Vue project This paper mainly records the process of building panorama in detail, so building Vue project is not described too much. 2、 Install three js npm install three –save npm install three-trackballcontrols –save npm install three-orbit-controls –save npm i three-obj-mtl-loader –save npm i three-fbx-loader –save npm i […]