Questions tagged [go-interface]
104 questions
                    
                    307
                    
            votes
                
                10 answers
            
        Type converting slices of interfaces
I'm curious why Go does't implicitly convert []T to []interface{} when it will implicitly convert T to interface{}.  Is there something non-trivial about this conversion that I'm missing?  
Example:
func foo([]interface{}) { /* do something */…
         
    
    
        danny
        
- 10,103
- 10
- 50
- 57
                    20
                    
            votes
                
                3 answers
            
        Interfaces and pointer receivers
I am newbie gopher and trying to get my head around the pointer receivers and interfaces. 
type Foo interface {
    foo()
}
type Bar struct {}
func (b *Bar) foo() {}
based on the above definitions..
--- Allowed ---------
b := Bar{}
b.foo()
--- Not…
         
    
    
        gopher
        
- 201
- 1
- 2
- 4
                    19
                    
            votes
                
                1 answer
            
        In Go, how do I check a value is of (any) pointer type?
I have a slice of interface{} and I need to check whether this slice contains pointer field values.
Clarification example:
var str *string
s := "foo"
str = &s
var parms = []interface{}{"a",1233,"b",str}
index :=…
         
    
    
        Elad
        
- 664
- 1
- 5
- 14
                    12
                    
            votes
                
                3 answers
            
        How to keep code DRY in Golang
EDIT++:
How to not to repeat my code in Go?
type Animal interface {
    Kingdom() string
    Phylum() string
    Family() string
}
type Wolf struct {}
type Tiger struct {}
func (w Wolf) Kingdom() string {return "Animalia"}
func (w Wolf) Phylum()…
         
    
    
        I159
        
- 29,741
- 31
- 97
- 132
                    10
                    
            votes
                
                4 answers
            
        In Golang, how can a consumer define an interface for a function that accepts an interface?
If I understand Go practices correctly, callers (aka consumers) are supposed to define interfaces of what they want to use from their dependencies (aka producers).
However, if the producer has a function that accepts a custom type, then it's better…
         
    
    
        Max Chernyak
        
- 37,015
- 6
- 38
- 43
                    9
                    
            votes
                
                4 answers
            
        Empty Interfaces in Golang
Edit: This is not the right way to use interfaces in Go. The purpose of this question is for me to understand how empty interfaces work in Go.
If all types in Go implement interface{} (empty interface), why can't I access the name field in the Cat…
         
    
    
        Eug
        
- 165
- 1
- 2
- 11
                    9
                    
            votes
                
                1 answer
            
        Convert Json.Number into int/int64/float64 in golang
I have a variable data, which is an interface. When I print its type I get it as json.Number. How do I type cast to int/int64/float64
If I try data.(float64), it ends up with panic error 
panic: interface conversion: interface {} is json.Number, not…
         
    
    
        KuZon
        
- 135
- 1
- 1
- 6
                    8
                    
            votes
                
                1 answer
            
        Understanding interface inside interface(Embedded Interface)
I was trying to understand the Interface embedding with the following code.
I have the following: 
type MyprojectV1alpha1Interface interface {
    RESTClient() rest.Interface
    SamplesGetter
}
// SamplesGetter has a method to return a…
         
    
    
        Invictus
        
- 2,653
- 8
- 31
- 50
                    7
                    
            votes
                
                1 answer
            
        How can I implement comparable interface in go?
I've recently started studying Go and faced next issue. I want to implement Comparable interface. I have next code:
type Comparable interface {
    compare(Comparable) int
}
type T struct {
    value int
}
func (item T) compare(other T) int {
    if…
         
    
    
        Orest
        
- 6,548
- 10
- 54
- 84
                    6
                    
            votes
                
                2 answers
            
        Go interface return type
I have an interface like this:
type ViewInterface interface{
    Init() View
}
type View struct{
    Width  int
    Height int
}
So I create a new type from View
type MainView View
func (m MainView) Init() MainView{
 return MainView{
  …
         
    
    
        Saeed M.
        
- 2,216
- 4
- 23
- 47
                    5
                    
            votes
                
                2 answers
            
        How to unite two different struct using interface?
I have the following code:
package main
import (
    "log"
)
type Data struct {
    Id int
    Name string
}
type DataError struct {
    Message string
    ErrorCode string
}
func main() {
    response := Data{Id: 100, Name: `Name`}
    if true…
         
    
    
        tsg
        
- 465
- 5
- 16
                    5
                    
            votes
                
                5 answers
            
        Modeling a hierarchy of related things without language support for type hierarchies
I'm new to Go, and one of the first things I want to do is to port my little marked-up-page-generation library to Go.  The primary implementation is in Ruby, and it is very much "classical object orientation" in its design (at least as I understand…
         
    
    
        Darshan Rivka Whittle
        
- 32,989
- 7
- 91
- 109
                    4
                    
            votes
                
                2 answers
            
        Constructor method in Interface? (in Golang)
If I have the following interface and struct:
package shape
type Shape interface {
    Area()
}
type Rectangle struct {
}
func (this *Rectangle) Area() {}
func New() Shape {
    return &Rectangle{}
}
Then how can I add the New() method (as a…
         
    
    
        lazywei
        
- 11,955
- 5
- 20
- 25
                    3
                    
            votes
                
                1 answer
            
        Golang map with any key type and any value type
can i create in golang a map with any key type and any value type ? , something like :
dict1 := map[interface]interface{}
Thanks a lot  !
         
    
    
        Edgar
        
- 43
- 1
- 4
                    3
                    
            votes
                
                2 answers
            
        Is it possible to cast map[string]string to map[string]interface{} without using for loop in golang?
When I try to convert map[string]string object to map[string]interface{} in golang using following snippet, I get an error.
package main
import "fmt"
func main() {
    var m = make(map[string]string)
    
    m["a"] = "b"
    
    m1 :=…
         
    
    
        Ruchit Patel
        
- 733
- 1
- 11
- 26