-3

What is Go's syntax to add item to a slice or an array?

package main

import "fmt"

type Car struct{
    Code string
    Brand string
    Type string
    Price int 
    Supply int
}

var Stock []Car

func init()  {
    Stock = []Car{
        Car{
            Code:"TOY13EMTAV",
            Brand:"Toyota Avanza",
            Type:"1.3 E M/T",
            Price:191100000,
            Supply:2,
        },
        Car{
            Code:"TOY15GMTAV",
            Brand:"Toyota Avanza",
            Type:"1.5 G M/T",
            Price:221250000,
            Supply:3,
        },
        Car{
            Code:"TOY15GCVTYAR",
            Brand:"Toyota Yaris",
            Type:"G CVT",
            Price:257650000,
            Supply:5,
        },
    }
}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • 1
    Hi, welcome and thanks for the question! Can you update your example with what you've tried so far so that we can get an idea of what you're trying to do? – Sam Whited Nov 09 '19 at 17:03
  • I've tried it this way @SamWhited ` func addStock(){ var new Car var totalArr int totalArr = len(Stock) fmt.Scanln(&new.Code,&new.Brand,&new.Type,&new.Price,&new.Supply) append(Stock[totalArr].Code,new.Code) }` but I get an error like this first argument to append must be slice; have string – Muhammad Avtara Khrisna Nov 09 '19 at 17:09
  • I've added an example that explains this; the first argument to append is indeed the slice itself. However, it looks like you might be trying to modify something in the slice, not actually append a new car to it? This was not clear from your original question. Is this what you're trying to do, or are you trying to append a new car to the slice? – Sam Whited Nov 09 '19 at 17:13
  • You can't add anything to an array. Arrays are fixed length. You can only change array elements. To add to a slice use `append`. – Jonathan Hall Nov 10 '19 at 09:06

1 Answers1

2

Data can be added to slices using the append builtin method. To append to a slice, pass the slice as an argument and assign the new slice back to the original. The details of why you have to do this aren't important if you're just learning the language, but suffice it to say that it makes things more efficient. For example:

Stock = append(Stock, Car{
  Code: "newcar",
  // …
})

You may also find this section from Effective Go on appending useful if you'd like a better understanding of append.

Here is a modified version of your example that can be run on the Go playground. I have made a few other minor changes to the code that may or may not be useful for a new developer to learn:

https://play.golang.org/p/AqlrL47pImw

Sam Whited
  • 6,880
  • 2
  • 31
  • 37
  • okay, now I have started to understand the way it works in go, thank you for helping a lot – Muhammad Avtara Khrisna Nov 09 '19 at 17:53
  • I'm glad this was helpful, good luck learning Go, it's a nice little language and I'm sure you'll enjoy it! If this answered your question, don't forget to mark it as the answer with the big green checkbox to help others in the future that might stumble upon this while searching. Thanks! – Sam Whited Nov 09 '19 at 21:03