How to get multiple value from user and stroe them in slice

I want to create a slice that can store values that are given by the user in run time. Total number of the string is not defined.

What have you tried so far and what is the problem you are facing with your current implementation?

package main

import (
“fmt”
)

func main() {
var n int
fmt.Scanf("%d", &n)
var ls []string
for i := 0; i < n; i++ {
a, _ := fmt.Scanf("%s", &i)
ls = append(ls, string(a))
fmt.Println(a)
}
fmt.Println(ls)
}

this my code, i understand that “&i” is wrong use, but i am unable to solve how what should i use in place of it which than pass the value to a and then it gets append to slice of string

solved using
func main() {
var n int
fmt.Scanln(&n)
var ls []string
reader := bufio.NewReader(os.Stdin)
for i := 0; i < n; i++ {
text, _ := reader.ReadString(’\n’)
ls = append(ls, text)
}
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.