title: Generic Method Parameters in Golang
body: <p>I need help with making this work for any type. </p>
  
  <p>I have got a function I need to accept other types that have <code>ID</code> property. </p>
  
  <p>I have tried using interfaces but that did not work for my <code>ID</code> property case. Here is the code:</p>
  
  <pre><code>package main
  
  
  import (
    "fmt"
    "strconv"
    )
  
  type Mammal struct{
    ID int
    Name string 
  }
  
  type Human struct {  
    ID int
    Name string 
    HairColor string
  }
  
  func Count(ms []Mammal) *[]string { // How can i get this function to accept any type not just []Mammal
     IDs := make([]string, len(ms))
     for i, m := range ms {
       IDs[i] = strconv.Itoa(int(m.ID))
     }
     return &amp;IDs
  }
  
  func main(){
    mammals := []Mammal{
      Mammal{1, "Carnivorious"},
      Mammal{2, "Ominivorious"},
    }
  
    humans := []Human{
      Human{ID:1, Name: "Peter", HairColor: "Black"},
      Human{ID:2, Name: "Paul", HairColor: "Red"},
    } 
    numberOfMammalIDs := Count(mammals)
    numberOfHumanIDs := Count(humans)
    fmt.Println(numberOfMammalIDs)
    fmt.Println(numberOfHumanIDs)
  }
  </code></pre>
  
  <p>I get this</p>
  
  <blockquote>
    <p>error prog.go:39: cannot use humans (type []Human) as type []Mammal in argument to Count</p>
  </blockquote>
  
  <p>See Go Playground for more details here <a href="http://play.golang.org/p/xzWgjkzcmH" rel="noreferrer">http://play.golang.org/p/xzWgjkzcmH</a></p>