Convert/Pass the contents of a struct to []String


New to go.

Working with some code, and overview is I would like to convert the contents of a struct, which is holding unmarshalled json into an array so it can be used in an API that only accepts []string format.

I have tried various ways of doing this, but being new I’m obviously missing something simple.

i.e. struct
type Log struct {
CommonLabels CommonLabels json:"commonLabels"

type CommonLabels struct {
name string json:"name"
note string json:"note"

So I have values in my struct and can print them if addressing them as etc

But I would like to have the name and note for example in a []string format. So I can pass the values to the matching field in the API.

cheers in advance


You could use a function:

func (cl CommonLabels) Slice() []string {
    return []string{, cl.note}

Thanks, I’ll look at this, I have managed to move things along by getting the labels out as, and Log.CommonLabels.note into an fmt.sprintf string then converting that into a slice

mylabelsfull := fmt.Sprintf(mylabels)
myslice := []string{mylabelsfull}

Being new I always struggle to call a function to manipulate code in go.

It May not be pretty but works for me.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.