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


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]

Updated on: 18-Mar-2021

716 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements