See https://golang.org/doc/faq#convert_slice_of_interface

V.

On Friday, 5 May 2017 13:35:42 UTC+2, eZio Pan wrote:
>
> Hi, I'm a newbie in Golang. I get stuck in a problem that I can't 
> understand why. I wish someone could help me out. Thanks!
>
> There are 2 struct type *S1* and *S2*. Both of them implement interface 
> type *StringI*.
> I write a function called *run* which use *StringI* interface as input, 
> and run all method *StringI* signatured.
>
> Then I write a test file, in which I declare a **S1* variable as *s1*, 
> and a **S2* variable as *s2*.
> I create a slice *[]interface{}{s1,s2}*, the I use for loop to get item 
> in it.
>
> But when I use *run*(*LoopedItem*), I got an error 
>
> *cannot use item (type interface {}) as type StringI in argument to run*
>
> And I use fmt.Printf("%T", *LoopedItem*), Golang does print right type -- 
> **S1* and **S2*
>
> Then I try type switch
>
> switch *LoopedItem*.(type){
> case *S1:
>     run(*LoopedItem*.(*S1))
> case *S2:
>     run(*LoopedItem*.(*S2))
> }
>
> Everything goes on well.
>
>
> *I wonder why I can't use *LoopedItem* (as StringI interface) as run 
> function's argument,but *LoopedItem*.(*TypePointer*) (as struct type 
> pointer) to the run function.*
>
>
>
> Here is the source code
>
>
> #### firstInter.go ####
>
> /*my first interface*/
> package firstInter
>
> import (
> "fmt"
> "strconv"
> )
>
> // interface with Get() and Push() method
> type StringI interface {
> Get() string
> Push(string)
> String() string
> }
>
> // type S1, store a int value
> type S1 struct {
> I int
> }
>
> // S1's Get method 
> // return the int value it stored
> func (s *S1) Get() string {
> return strconv.Itoa(s.I)
> }
>
> // S1's Push method
> // put new value into S1
> func (s *S1) Push(a string) {
> num, err := strconv.Atoi(a)
> if err == nil {
> s.I = num
> }
> }
>
> // S1's String method
> // for fmt
> func (s *S1) String() string {
> return strconv.Itoa(s.I)
> }
>
> // type S2, store a string value
> type S2 struct {
> S string
> }
>
> // S2's Get method 
> // return the string value it stored
> func (s *S2) Get() string {
> return s.S
> }
>
> // S2's Push method
> // put new value into S2
> func (s *S2) Push(a string) {
> s.S = a
> }
>
> // S2's String method
> // for fmt
> func (s *S2) String() string {
> return s.S
> }
>
> // function to run every method
> // StringI interface signatured
> func run(tI StringI) {
> fmt.Printf("%v\n", tI.Get())
> tI.Push("3")
> fmt.Printf("%v\n", tI.Get())
> }
>
>
> #### firstInter_test.go ####
>
> // firstInter package Unit Testing
> package firstInter
>
> import (
> //"fmt"
> )
>
> var tI StringI
>
> var s1 *S1 = &S1{1}
> var s2 *S2 = &S2{"???"}
> var l []interface{} = []interface{}{s1, s2}
>
> func Example_InterFace() {
> for _, item := range l {
> // something wrong here
> // run(item)
> // fmt.Printf("%T\n", item)
> switch item.(type) {
> case *S1:
> run(item.(*S1))
> case *S2:
> run(item.(*S2))
> }
> }
> // Output:
> // 1
> // 3
> // ???
> // 3
> }
>
>

-- 
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