Golang Program to create a string array that takes inputs from users.

Go ProgrammingServer Side ProgrammingProgramming

Example

Approach

  • Ask the user to enter the size of array.

  • Make a string array of given size.

  • Ask the user to enter the elements.

  • At the end, print the array.

Example

 Live Demo

package main
import (
   "fmt"
)
func main(){
   fmt.Printf("Enter size of your array: ")
   var size int
   fmt.Scanln(&size)
   var arr = make([]string, size)
   for i:=0; i<size; i++ {
      fmt.Printf("Enter %dth element: ", i)
      fmt.Scanf("%s", &arr[i])
   }
   fmt.Println("Your array is: ", arr)
}

Output

Enter size of your array: 6
Enter 0th element: g
Enter 1th element: o
Enter 2th element: l
Enter 3th element: a
Enter 4th element: n
Enter 5th element: g
Your array is: [g o l a n g]
raja
Published on 18-Mar-2021 12:22:38
Advertisements