Algorithm


For Example use the scan() function to take input from the user

Code Examples

#1 Example- Go Take Input

Code - Golang Programming

package main
import "fmt"

func main() {
  var name string

  // takes input value for name
  fmt.Print("Enter your name: ")
  fmt.Scan(&name)

  fmt.Printf("Name: %s", name)

}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter your name: Akash
Name: Akash

#2 Go fmt.Scan()

Code - Golang Programming

package main
import "fmt"

func main() {
  var language string

  // takes input value for name
  fmt.Print("Enter your favorite language: ")
  fmt.Scan(&language)

  fmt.Printf("Favorite Language: %s", language)

}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter your favorite language: Go Programming Favorite Language: Go

#3 Multiple Inputs Using Scan()

Code - Golang Programming

package main
import "fmt"

func main() {
    
  // create variables
  var name string
  var age int
    
  // take name and age input
  fmt.Println("Enter your name and age:")
  fmt.Scan(&name, &age)
    
  // print input values
  fmt.Printf("Name: %s\nAge: %d", name, age)

} 
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter your name and age: Akash 27 Name: Akash Age: 27

#4 Go fmt.Scanln()

Code - Golang Programming

package main
import "fmt"

func main() {
  var name string
  var age int
    
  // take name and age input
  fmt.Println("Enter your name and age:")
  fmt.Scanln(&name, &age)
    
  fmt.Printf("Name: %s\nAge: %d", name, age)

}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter your name and age: Akash Name: Akash Age: 0

#5 Go fmt.Scanln()

Code - Golang Programming

package main
import "fmt"

func main() {
  var name string
  var age int
    
  // take name and age input
  fmt.Println("Enter your name and age:")
  fmt.Scanln(&name, &age)
    
  fmt.Printf("Name: %s\nAge: %d", name, age)

}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter your name and age: Akash Name: Akash Age: 0
Advertisements

Demonstration


Go Take Input-Go Lang Programming