Cannot use "a" (type string) as type in array element in go

Issue

I’m pretty new to Golang and I have an issue with adding items to Array.

I use this link as a reference golang-book.

I have this struct:

package models

type FileMD struct {
    fileName string
    fileSize int
}

I tried to do it both ways but I failed.

fileList := [...]models.FileMD{"a", 1: "b", 2}
var fileList [...]models.FileMD
fileList[0] = "a", 1

What is the correct way?

Solution

I am not sure but I think you are looking for:

fileList[0] = FileMD{"a", 1}

Or maybe:

fileList := []FileMD{{"a", 1}, {"b", 2}}

Answered By – cnicutar

Answer Checked By – Marie Seifert (GoLangFix Admin)

Leave a Reply

Your email address will not be published.