How to range over []interface{}
up vote
-1
down vote
favorite
I have this interface{}, resulting from a json unmarshal.
(interface {}) (len=2 cap=2) {
(map[string]interface {}) (len=2) {
(string) (len=6) "@value": (string) (len=10) "Repository",
(string) (len=9) "@language": (string) (len=2) "en"
},
(map[string]interface {}) (len=2) {
(string) (len=6) "@value": (string) (len=7) "Ystorfa",
(string) (len=9) "@language": (string) (len=5) "cy-GB"
}
}
How can I range over the slice? If I simply try: for _, c := range Container {
I got the error:
cannot range over Container (type interface {})
go
add a comment |
up vote
-1
down vote
favorite
I have this interface{}, resulting from a json unmarshal.
(interface {}) (len=2 cap=2) {
(map[string]interface {}) (len=2) {
(string) (len=6) "@value": (string) (len=10) "Repository",
(string) (len=9) "@language": (string) (len=2) "en"
},
(map[string]interface {}) (len=2) {
(string) (len=6) "@value": (string) (len=7) "Ystorfa",
(string) (len=9) "@language": (string) (len=5) "cy-GB"
}
}
How can I range over the slice? If I simply try: for _, c := range Container {
I got the error:
cannot range over Container (type interface {})
go
1
You have a slice wrapped in aninterface{}
value. First do a type assertion to obtain the slice value, which you can rage over afterwards. E.g.for _, c := range Container.(interface{})
. Note: error check (if it is notnil
or if it truly is a slice) is omitted.
– icza
Nov 22 at 10:41
no, i can't do this toocannot range over Container.(interface {}) (type interface {})
– George Livanoss
Nov 22 at 10:51
3
Sorry, it should be:Container.(interface{})
– icza
Nov 22 at 10:58
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have this interface{}, resulting from a json unmarshal.
(interface {}) (len=2 cap=2) {
(map[string]interface {}) (len=2) {
(string) (len=6) "@value": (string) (len=10) "Repository",
(string) (len=9) "@language": (string) (len=2) "en"
},
(map[string]interface {}) (len=2) {
(string) (len=6) "@value": (string) (len=7) "Ystorfa",
(string) (len=9) "@language": (string) (len=5) "cy-GB"
}
}
How can I range over the slice? If I simply try: for _, c := range Container {
I got the error:
cannot range over Container (type interface {})
go
I have this interface{}, resulting from a json unmarshal.
(interface {}) (len=2 cap=2) {
(map[string]interface {}) (len=2) {
(string) (len=6) "@value": (string) (len=10) "Repository",
(string) (len=9) "@language": (string) (len=2) "en"
},
(map[string]interface {}) (len=2) {
(string) (len=6) "@value": (string) (len=7) "Ystorfa",
(string) (len=9) "@language": (string) (len=5) "cy-GB"
}
}
How can I range over the slice? If I simply try: for _, c := range Container {
I got the error:
cannot range over Container (type interface {})
go
go
edited Nov 22 at 11:24
Flimzy
36.7k96496
36.7k96496
asked Nov 22 at 10:40
George Livanoss
183
183
1
You have a slice wrapped in aninterface{}
value. First do a type assertion to obtain the slice value, which you can rage over afterwards. E.g.for _, c := range Container.(interface{})
. Note: error check (if it is notnil
or if it truly is a slice) is omitted.
– icza
Nov 22 at 10:41
no, i can't do this toocannot range over Container.(interface {}) (type interface {})
– George Livanoss
Nov 22 at 10:51
3
Sorry, it should be:Container.(interface{})
– icza
Nov 22 at 10:58
add a comment |
1
You have a slice wrapped in aninterface{}
value. First do a type assertion to obtain the slice value, which you can rage over afterwards. E.g.for _, c := range Container.(interface{})
. Note: error check (if it is notnil
or if it truly is a slice) is omitted.
– icza
Nov 22 at 10:41
no, i can't do this toocannot range over Container.(interface {}) (type interface {})
– George Livanoss
Nov 22 at 10:51
3
Sorry, it should be:Container.(interface{})
– icza
Nov 22 at 10:58
1
1
You have a slice wrapped in an
interface{}
value. First do a type assertion to obtain the slice value, which you can rage over afterwards. E.g. for _, c := range Container.(interface{})
. Note: error check (if it is not nil
or if it truly is a slice) is omitted.– icza
Nov 22 at 10:41
You have a slice wrapped in an
interface{}
value. First do a type assertion to obtain the slice value, which you can rage over afterwards. E.g. for _, c := range Container.(interface{})
. Note: error check (if it is not nil
or if it truly is a slice) is omitted.– icza
Nov 22 at 10:41
no, i can't do this too
cannot range over Container.(interface {}) (type interface {})
– George Livanoss
Nov 22 at 10:51
no, i can't do this too
cannot range over Container.(interface {}) (type interface {})
– George Livanoss
Nov 22 at 10:51
3
3
Sorry, it should be:
Container.(interface{})
– icza
Nov 22 at 10:58
Sorry, it should be:
Container.(interface{})
– icza
Nov 22 at 10:58
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429083%2fhow-to-range-over-interface%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You have a slice wrapped in an
interface{}
value. First do a type assertion to obtain the slice value, which you can rage over afterwards. E.g.for _, c := range Container.(interface{})
. Note: error check (if it is notnil
or if it truly is a slice) is omitted.– icza
Nov 22 at 10:41
no, i can't do this too
cannot range over Container.(interface {}) (type interface {})
– George Livanoss
Nov 22 at 10:51
3
Sorry, it should be:
Container.(interface{})
– icza
Nov 22 at 10:58