Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append to a 2d slice

Tags:

slice

append

go

2d

I have data that is created rows by rows, 6 columns, I don't know the final number of rows in advance.

Currently i'm creating a 2D slice of 200x6 with all zeros and then i replace these zeros gradually with my data, row by row. The data comes from another dataframe df

It works but i don't like to end up with the last rows of my slice full of zeros. I see 2 solutions: - I delete all the last rows with only zeros when I'm done - I create an empty slice and append my data progressively to it

I tried various things but could not figure out how to code any of these 2 solutions.

Currently my code looks like this:

var orders [200][6]float64  // create my 2d slice with zeros
order_line := 0

for i := start_line; i <= end_line; i++ {
    if sell_signal == "1" {
        //record line number and sold price in orders slice
        orders[order_line][1] =  float64(i+1)
        orders[order_line][2],err = strconv.ParseFloat(df[i][11], 64)
        order_line = order_line + 1
     }
}

I looked at the Append command, but I tried all sorts of combinations to make it work on a 2d slice, could not find one that works.

edit: from the comments below I understand that i'm actually creating an array, not a slice, and there is no way to append data to an array.

like image 691
Hugues Avatar asked Dec 10 '25 17:12

Hugues


1 Answers

In Golang slices are preferred in place of arrays.

Creating so many rows in prior is not required, just create a slice every time you are looping over your data to add a new row in the parent slice. That will help you to have only required number of rows and you need to worry about the length Since you are appending a slice at an index of parent slice.

package main

import (
    "fmt"
    "math/rand"
)

func main() {
    orders := make([][]float64, 0) // create my 2d slice with zeros
    for i := 0; i <= 6; i++ {
        value := rand.Float64()
        temp := make([]float64, 0)
        temp = append(temp, value)
        orders = append(orders, [][]float64{temp}...)
    }
    fmt.Println(orders)
}

Please check working code on Playground

If you notice I am creating a new temp slice in loop which contains the float64 value and then appending value to the temp slice which I have passed to the parent slice.

So everytime I append the temp slice to the parent slice a new row will be created.

Note:

Arrays have their place, but they're a bit inflexible, so you don't see them too often in Go code. Slices, though, are everywhere. They build on arrays to provide great power and convenience.

Edited:

To work on first 3 columns and then manipulate the values for next 3 columns which will be added to the temp slice and appended to the main slice. Use below code logic:

package main

import (
    "fmt"
    "math/rand"
    "strconv"
)

func main() {
    orders := make([][]float64, 0) // create my 2d slice with zeros
    for i := 0; i <= 6; i++ {
        value := rand.Float64()
        // logic to create first 3 columns
        temp := make([]float64, 0)
        temp = append(temp, value)

        temp2 := make([]float64, 3)

        // logic to create next 3 columns on basis of previous 3 columns
        for j, value := range temp {
            addcounter, _ := strconv.ParseFloat("1", 64)
            temp2[j] = value + addcounter
        }

        temp = append(temp, temp2...)
        orders = append(orders, [][]float64{temp}...)
    }
    fmt.Println(orders)
}

Working Example

like image 180
Himanshu Avatar answered Dec 12 '25 14:12

Himanshu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!