#completely new to go, learning to make a random number list generator

23 messages · Page 1 of 1 (latest)

mellow jewel
#

this is what I've got so far, the print statements are just there to see what the list looks like

package main

import(
    "fmt"
    "math/rand"
)


func main() {
    fmt.Println("Hello world")

    var randomList [10]int

    randomList = [10]int(generateRandArray(len(randomList), 100)) // not entirely sure of the [10]int does here, gopls inserted it (thanks)


}

func generateRandArray(listLength int, maxNum int) []int { // here i think the []int describes the return type but not entirely sure
    returnList := make([]int, listLength)

    fmt.Print("[")
    for i := 0; i<listLength-1; i++{
        returnList[i] = rand.Intn(maxNum)
        fmt.Print(returnList[i], ",")
    }
    returnList[listLength-1] = rand.Intn(10)
    fmt.Print(returnList[listLength-1])
    fmt.Println("]")
    
    return returnList
}

So far it works but I have like 2 comments of things im not sure about, help and criticism is appreciated

echo wing
#

Hey, nice work! And welcome to the community!

#

I have a couple quick observations to share, but before I do I just want to gently point your attention (if you have not seen it) at the Tour of Go which is the canonical entrypoint for all new gophers: https://go.dev/tour/welcome/1

#

Everything I'm about to tell you will have been covered by the Tour as well, which is why I recommend you start there if you have not done so already.

#

Regarding [10]int: Go has two different sequential types "arrays" and "slices". An array is a fixed-length chunk of contiguously allocated memory. [10]int reads precisely as "an array of 10 integers". Two important notes about arrays:

  1. They are fixed size - an array cannot change size. If you want an array of a different size, you have to allocate a new array and copy data from the original array over.
  2. The size is part of the type. This means an value of type [10]int is a different type than a value of type [9]int, despite superficially being just "arrays of ints".
    Particularly that second point can make arrays very difficult to use and thus they are rarely used directly in application code. Sometimes you know precisely the size of the data you're working with (such as when decoding some binary format) but more often than not you're dealing with sequential data of various sizes, and it would be quite painful to have to pass around typed arrays all over the place.
#

Enter slices, the far far more common sequential data type you'll use in Go. Slices are similar to arrays (in fact under the hood they maintain a pointer to one) but they're much more flexible because they can be seamlessly resized. Slice types don't specify a size, so a slice of ints would be typed as []int (note the lack of number between the square brackets). The return type of your function func generateRandArray(listLength int, maxNum int) []int is a slice of ints []int.

#

So to recap:

  • 2 different sequential types in Go: arrays and slices
  • Arrays are fixed length and contiguously allocated regions of memory. They are rarely used directly, mainly due to the difficulty in having to specify their length as part of their type.
  • Slices can be thought of as dynamic arrays (they handle resizing under the hood for you) and are what you'll use 99 times out of 100. They do not specify a length as part of their type.
#

Since you mentioned

var randomList [10]int
randomList = [10]int(generateRandArray(len(randomList), 100)) // not entirely sure of the [10]int does here, gopls inserted it (thanks)

I just want to clarify what's happening here. randomList has type [10]int. generateRandArray(len(randomList), 100) returns an []int as seen by its signature:

func generateRandArray(listLength int, maxNum int) []int

[]int and [10]int are different types. As discussed above, the first is a slice and the second is an array. Because they're different types, the result of generateRandArray(len(randomList), 100) is not assignable to randomList which expects a [10]int, not a []int. Thus it's necessary to do a conversion from []int to [10]int. To do that, you use the conversion syntax T(v) to convert v's type to T. In this case, you're using [10]int(...) to convert the result of generateRandArray(len(randomList), 100) to a [10]int from an []int.

mellow jewel
#

The size is part of the type. This means an value of type [10]int is a different type than a value of type [9]int, despite superficially being just "arrays of ints".
Oh, that makes more sense

mellow jewel
#

thanks man that was really helpful

mellow jewel
#

baby steps but i just made a linear search we up 💯 🔥

fallow flax
#

append is a built-in, but it's not doing anything you couldn't do yourself using make and copy. If you feel the need to change the formula, you can do so

#

It's generally pretty good though. And you can always pre-allocate using make, if you know how many elements you're going to be appending

#

The current implementation is to double the capacity up to a given threshold (256). After that, it multiplies the old capacity by 1.25x