3

I'm brand new to Go and having trouble getting fmt.scan() to fill a slice. The number of input values is dynamic and I can't use a for loop. My initial thought was to try this:

package main

import "fmt"

func main() {
    var x []int
    fmt.Println("Enter input")
    fmt.Scanf("%v", append(x))
    fmt.Println(x)
}

Which obviously doesn't work. Can someone point me in the right direction?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
user57501
  • 33
  • 1
  • 3
  • Possible duplicate of [Read numbers from os.Stdin into array or slice in Go](https://stackoverflow.com/questions/48943178/read-numbers-from-os-stdin-into-array-or-slice-in-go/48944099#48944099). – icza Jun 16 '18 at 07:19
  • @icza: The question says "I can't use a for loop." – peterSO Jun 16 '18 at 13:45
  • @peterSO You're right, in which case your solution should be accepted. – icza Jun 16 '18 at 13:51

3 Answers3

3

[Get] fmt.Scan() to fill a slice. The number of input values is dynamic and I can't use a for loop.


Perhaps, something like this:

package main

import "fmt"

func input(x []int, err error) []int {
    if err != nil {
        return x
    }
    var d int
    n, err := fmt.Scanf("%d", &d)
    if n == 1 {
        x = append(x, d)
    }
    return input(x, err)
}

func main() {
    fmt.Println("Enter input:")
    x := input([]int{}, nil)
    fmt.Println("Input:", x)
}

Output:

Enter input:
 1
2 3
4
 5  6  7

Input: [1 2 3 4 5 6 7]

ADDENDUM:

When storage is allocated for a variable or a new value is created, and no explicit initialization is provided, the variable or value is given a default value, the zero value for its type: nil for slices. Conversions are expressions of the form T(x) where T is a type and x is an expression that can be converted to type T. []int(nil) is a conversion to the zero value for the slice value []int.

x := input([]int(nil), nil)

is equivalent to

x := input([]int{}, nil)

or

var x []int
x = input(x, nil)

I have revised my answer to use:

x := input([]int{}, nil)
peterSO
  • 158,998
  • 31
  • 281
  • 276
  • Thanks, this looks perfect! Would you mind explaining the purpose of saying "[]int(nil)", specifically the (nil) portion? I understand every other part of your snippet. – user57501 Jun 16 '18 at 04:27
  • @user57501: See the addendum to my answer. – peterSO Jun 16 '18 at 05:20
1

I'm new to Go, so this are my 2cents as a newbie.

func main(){
    var numsToInput int 

    fmt.Println("Welcome user!")
    fmt.Println("How many numbers would you like to scale today?")
    fmt.Scan(&numsToInput)

    fmt.Println("Type please the ", num, " numbers: ")

    var values []float32 // Empty slice
    
    for i := 0; i < num; i++{
        var val float32
        fmt.Scanln(&val)
        values = append(values, val)
    }
    fmt.Println(values)
}

It's not a very elaborate program, but certainly it's simple.

I hope it was useful.

0

Using simple packages and more logic, you could try this,

package main

import "fmt"

func main() {
    var ele rune
    var size int
    var sli = make([]int,0,1)
    size = cap(sli)
    for i:=0; i<=size; i++{
        if i>=len(sli){
            size=size+1
        }
        ele = 0
        fmt.Println("Enter a number to add: ")
        fmt.Scan(&ele)
        if ele==0 {
            fmt.Println("Stopping!")
            break
        }
        sli = append(sli, int(ele))
    }
    fmt.Println(sli)
}

The code would stop and print the slice when you enter anything other than an integer.