convert type []interface {} to type []string

13,407 views
Skip to first unread message

Tong Sun

unread,
Jun 28, 2015, 5:46:14 PM6/28/15
to golan...@googlegroups.com
I got "need type assertion" error when trying to convert interface {} array to string array. 

Having looked at the following two answers, 


I'm still can't get my special case working -- I'm Unmarshaling the following yaml into a `map[interface{}]interface{}` structure:

const data = `
Colors:
  - red
  - blue
  - white
`

m := make(map[interface{}]interface{})

err := yaml.Unmarshal([]byte(data), &m)

The unmarshalled data is apparently some kind of string array. How can tell Go to treat them so? 

The code is kind of long, but I've got the part up to "fmt.Println("\nNext")" working. It is the next two lines I'm struggling to get working. 

Please help. 

PS. Full source code also available at http://pastie.org/10263472

Thanks

package main

import (
	"fmt"
	"os"
	"strings"
	"text/template"

	"gopkg.in/yaml.v2"
)

const data = `
Colors:
  - red
  - blue
  - white
`

const templ1 = `{{range .Colors}}{{.}}, {{end}}`
const templ2 = `{{join .Colors}}`

func main() {

	m := make(map[interface{}]interface{})

	err := yaml.Unmarshal([]byte(data), &m)
	checkError(err)
	fmt.Printf("--- m:\n%v\n\n", m)

	d, err := yaml.Marshal(&m)
	checkError(err)
	fmt.Printf("--- m dump:\n%s\n\n", string(d))

	t := template.New("Test template")
	t, err = t.Parse(templ1)
	checkError(err)

	err = t.Execute(os.Stdout, m)
	checkError(err)

	colors := []string{"red", "white", "blue"}
	t = template.Must(template.New("").Funcs(funcs).Parse(templ2))
	err = t.Execute(os.Stdout, struct{ Colors []string }{colors})
	checkError(err)

	fmt.Println("\nNext")
	fmt.Println(strings.Join(m["Colors"].([]string), ", "))
	err = t.Execute(os.Stdout, struct{ Colors []string }{m["Colors"].([]string)})
	checkError(err)

}

var funcs = template.FuncMap{
	"join": func(s []string) string { return strings.Join(s, ", ") },
}

func checkError(err error) {
	if err != nil {
		fmt.Println("Fatal error ", err.Error())
		os.Exit(1)
	}
}



Tong Sun

unread,
Jun 28, 2015, 6:07:48 PM6/28/15
to golang-nuts

On Sun, Jun 28, 2015 at 5:46 PM, Tong Sun <sunto...@gmail.com> wrote:
The code is kind of long, but I've got the part up to "fmt.Println("\nNext")" working. It is the next two lines I'm struggling to get working. 

I didn't mean that the custom template function `join` has to keep its interface, I was just trying to show it was working before. What ever will make the next two lines work will do. 

Thanks

Kiki Sugiaman

unread,
Jun 28, 2015, 8:15:52 PM6/28/15
to golan...@googlegroups.com
Unfortunately you can only type assert the way you did for interface{}.
For []interface{}, you have to do it element by element.

http://play.golang.org/p/Nq8efe5eWh

Tamás Gulácsi

unread,
Jun 29, 2015, 12:38:39 AM6/29/15
to golan...@googlegroups.com
Why can't you unmarshal into a map[string]interface{} ?

Tong Sun

unread,
Jun 29, 2015, 9:35:51 AM6/29/15
to golang-nuts
That's a good idea. 

Hmm..., on second thought, that wouldn't matter, would it? Because I'll still end up with an interface {} array right? 

On Mon, Jun 29, 2015 at 12:38 AM, Tamás Gulácsi <tgula...@gmail.com> wrote:
Why can't you unmarshal into a map[string]interface{} ?

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/V6cO7aq1ZWg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages