Questions tagged [go-interface]
104 questions
0
votes
1 answer
Store a collection of constructors for types that all conform to the same interface
I'm making an app that'll need sets of rules to run a job. The app offers the possibility to express the rules in one of several different languages. I therefore have defined an interface to a live rules engine, that offers the methods that the app…

Mathias Dolidon
- 3,775
- 1
- 20
- 28
0
votes
1 answer
How to get attribute from a interfce instance from struct in Go
I want to get the v.val, but the go compiler throw me an Error:
v.val undefined (type testInterface has no field or method val)
but in the v.testMe method,It work.
package main
import (
"fmt"
)
type testInterface interface {
…

user1458435
- 3
- 2
0
votes
1 answer
En/Decode struct containing many interfaces with different implementations each with gob
I have a quite complex struct that contains many interfaces with each different implementations. For en/decoding that struct in gob I seem to have to register every implementation that could be possibly used for every interface. So I end up with a…

panmari
- 3,627
- 3
- 28
- 48
0
votes
1 answer
set variable of any struct passed as interface{}
I would like to know how to set a variable using reflection when using an interface{} value, and all kind of structs could be passed to func F(o interface{}). How to change the first value (s.A) to 'hello'?
package main
import (
"fmt"
…

Klau3
- 317
- 1
- 2
- 9
0
votes
1 answer
Golang interfaces to simplify dependencies?
Hmmm, I'm having a problem wrapping my head around interfaces.
So I am using a Go package for handling my mongodb stuffs, but I don't want to import that package into every model and what not. I'd like to keep as many of my sub packages (like…

macb
- 41
- 1
- 7
-1
votes
1 answer
In golang how to loop through string interface
I have an interface that has some strings. I want to print each item one by one. For example, consider the following interface.Here I want to print sud first then man
var x interface{} = []string{"sud", "man"}

randomDev
- 73
- 9
-1
votes
1 answer
How can I reach struct member in interface type
I have to keep multi type struct in slice and seed them. I took with variadic parameter of interface type and foreach them. If I call the method of interface it works, but when I trying to reach to struct I can't. How can I solve that?
Note: Seed()…

icsarisakal
- 193
- 7
-1
votes
1 answer
How does resp.Body implements the Read function?
I'm currently studying Go and trying to understand the concept of Interfaces.
What I understand about it is that whatever type implements the functions specified in the interface is part of it.
My question is about the Body of type io.ReadCloser…

Rodrigo Felipe
- 13
- 2
-1
votes
1 answer
Go append a string to an array of array of interface
Trying to add a string to an initialised array of array of interfaces.
matrix := [][]interface{}{{"cat", "cat", "cat"}}
a := [][]interface{}{{"tiger"}}
matrix = append(matrix, a...)
Output:
[[cat cat cat] [tiger]]
But I wanted tiger in together…

aakash singh
- 267
- 5
- 19
-1
votes
1 answer
Golang compare and update keys from two different map string interfaces
After unmarshalling two yaml files to two different maps, i wanted to compare keys (both outer and inner keys as it is a nested map) of both maps and if any key (outer or inner key) is present in first map 'configMap' and absent in second map…

sjsri
- 63
- 1
- 8
-1
votes
1 answer
Golang Interface{} wont type assert to int
I’m working on a rest api but for some reason can't type assert an interface{} to its underlying type - int.
I send data via post request, to create an ad. It looks like so:
POST http://localhost:8080/api/ads
{
"Title": "Example…

Bryan Carty
- 1
- 2
-1
votes
1 answer
Golang: Why sql.Tx does not implement driver.Tx
Why in the following example compiler says sql.Tx does not implement driver.Tx, seeing that sql.Tx does indeed fulfil the interface:
import (
"database/sql"
"database/sql/driver"
)
func main() {
var myDB store = db{}
}
type store…

Cae Vecchi
- 868
- 1
- 10
- 19
-1
votes
1 answer
Could a method return a pointer with return type of this method is value
I saw a piece of code as below:
Just wondering as the value method of draw() have been implemented, why could it return the pointer of the struct in fact.
type Shape interface {
draw()
}
type Rectangle struct {
}
func (Rectangle) draw() {
…

s666
- 266
- 1
- 3
- 14
-1
votes
1 answer
Go: use slice of different numeric types as field of a struct
I am writing a collector that collects metrics and stores in structs that looks something like this:
type Metric struct {
Name string
Data []float64
}
However for some metrics, it does not make sense to use float64, since their values are…

vgratian
- 45
- 9
-1
votes
1 answer
Mock interface return type for dep injection
EDIT
As pointed out in the accepted answer, the issue here was doing go duck typing in the wrong direction. I'd like to add the following github issue as an attachment, since it provided me useful information in addition to @matt answer…

KawaLo
- 1,783
- 3
- 16
- 34