如何在 Golang 获取用户输入?

Scanln 功能可用于在 Golang 中接受用户的输入。下面是从用户处获取输入的示例:

// Golang program to show how
// to take input from the user
package main

import "fmt"

// main function
func main() {

    // Println function is used to
    // display output in the next line
    fmt.Println("Enter Your First Name: ")

    // var then variable name then variable type
    var first string

    // Taking input from user
    fmt.Scanln(&first)
    fmt.Println("Enter Second Last Name: ")
    var second string
    fmt.Scanln(&second)

    // Print function is used to
    // display output in the same line
    fmt.Print("Your Full Name is: ")

    // Addition of two string
    fmt.Print(first + " " + second)
}

现在保存该文件并执行,如下图所示:

How to take input from the user in Golang

关于节目的描述:

go
 var var_name data_type