Hi All,

(S(d)).testUnderlyingTypeReciever()

Regards

El miércoles, 8 de noviembre de 2017, 18:44:36 (UTC-3), Haiyu Zhen escribió:
>
> As someone who is new to Golang world, I am confused with the following 
> code.
>
> As sample code shown, type S's "underlying" type is slice (in C++ jargon S 
> is the derived class of slice), and if I pass a slice as S in function 
> parameter, it works; but if I pass slice as S in function receiver, Go 
> doesn't "downcast" it to S.
> I do know that S and []int are treated as different types in Go, but as 
> the append function shows, Go will "upcast" S to slice to use 
> slice.append(). 
>
> So when should I expect the type casting to work? Or or specific to the 
> scenario, why it fails if I use slice as the function receiver for S?
>  
>
>> package main
>> import (
>> "fmt"
>> )
>> type S []int
>>  
>> func (s S) testUnderlyingTypeAsReciever() {
>> fmt.Println("Hello, playground")
>> }
>> func testUnderlyingTypeAsParam(s S) {
>> fmt.Println("Hello, playground")
>> }
>> func main() {
>> d := []int{2}
>> s := S{}
>> s = append(s, 1) // use append function for slice
>> testUnderlyingTypeAsParam(d) // pass
>> d.testUnderlyingTypeAsReciever() // fail
>> }
>
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to