Type of t: main.Type1
Type of v: main.Type2
{type2}
{type1 32}
you have ignored the boolean returned from thebvtype assertion, which will be false. v will be the zero value of type2.
chris onna bus
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Jan, the receiver Type1 should be pointer; then try. Also I mentioned the link that runs http://play.golang.org/p/7UrKzGQaIp , didn't I?
What comes out of type assertion is a copy! is new learning for me. I thought interface{} hold type info and value object and type assertion would cast to that object if proper type is used for assertion.
Why a copy?
If I want to pass objects for processing into func that takes interface{} then changes will be done in copy and I need to return the copy by pointer then in such cases?
Pointer methods can also be used to prevent copy in case the object is big enough.
Also I can call the method anyway on next line so why the compiler not compile things as if done separately?
What comes out of type assertion is a copy! is new learning for me. I thought interface{} hold type info and value object and type assertion would cast to that object if proper type is used for assertion. Why a copy? If I want to pass objects for processing into func that takes interface{} then changes will be done in copy and I need to return the copy by pointer then in such cases?
I like most the implicit interface implementation however when looked into interface{} then it was not as clear. Like the 'fmt' package has many functions like Println, Fprintln, etc that takes interface{} and then inside there is a check for whether a passed in type has implemented 'Stringer' interface. By looking at the doc (http://golang.org/pkg/fmt/#Println) I get no idea that interface 'Stringer' is honored inside. How do I know such things.
For each Printf-like function, there is also a Print function that
takes no format and is equivalent to saying %v for every
operand. Another
variant Println inserts blanks between operands and appends a newline.
Under General the overview says:From Martin's comment it is clear to me now that if you pass struct value you will get copy, if you pass it by reference the interface holds reference (pointer copy) hence pointing to same object.
With empty interface{} I am not thinking about best practices rather experimenting to understand the interface{} implementation. What bothered me is a runtime check is required to find it something passed as interface{} implements other concrete interface or types.
go doesn't have generic type template func likefunc Filter(t T) bool{if t.count > 10{ //t.count field check should be at compile time when func Filter is calledreturn true}else{return false}}Then call Filter on any type that should have 'count' fieldtype data struct{count int}Filter(data{count:10}) //compiler should generate copy of Filter func that satisfies type data
I like most the implicit interface implementation however when looked into interface{} then it was not as clear. Like the 'fmt' package has many functions like Println, Fprintln, etc that takes interface{} and then inside there is a check for whether a passed in type has implemented 'Stringer' interface. By looking at the doc (http://golang.org/pkg/fmt/#Println) I get no idea that interface 'Stringer' is honored inside. How do I know such things. I got to know from slides and blogs about Println using Stringer interface.
Am Mittwoch, 8. Januar 2014 12:12:05 UTC+1 schrieb Abhijit Kadam:From Martin's comment it is clear to me now that if you pass struct value you will get copy, if you pass it by reference the interface holds reference (pointer copy) hence pointing to same object.In Go there is no such thing like "pass by reference", only passby value.