Questions tagged [go-interface]
104 questions
0
votes
1 answer
Unable to write a generic function that can work on multiple Structs in Golang
I am new to Go, and learning Interfaces and Structs. I am having 2 structs - ServiceSection and SliderSection and I am trying to accomplish the below 2 tasks with each of them-
GET the JSON response and unmarshal it.
Use the struct to create HTML…

Rahul Satal
- 2,107
- 3
- 32
- 53
0
votes
1 answer
Custom UnmarshalJSON not being called
I have an interface that's implemented by a few structs. On one of my methods, I instantiate the struct base on a given string and then parse a json into the defined struct. To do so, I have implemented UnmarshalJSON but it's never being…

HerbertRedford
- 230
- 4
- 14
0
votes
2 answers
Remove an interface item from a slice
I want to remove an item in a slice w/o having to use a specific function for every type of items in the slice. So, I am using interface{} as the slice item type:
package main
import "fmt"
func sliceRemoveItem(slice []interface{}, s int)…

Juan Ignacio Pérez Sacristán
- 604
- 1
- 4
- 20
0
votes
1 answer
Go interface: interface not implemented even though it is
Can someone explain to me why this kind of implementation is not allowed in go?
I have a function that takes the interface the function is defined in as an argument. This throws an error.
package main
import (
"fmt"
)
type Anode int
func (a…

Akhil Sasidharan
- 29
- 3
0
votes
3 answers
Golang iterate over map of interfaces
I am trying to iterate over a map of interfaces in golang, it has the below structure, I am able to use for loop to iterate to a single level but couldn't go deep to get values of the interface.
Yaml
steps:
execute:
- mvn : 1.9.3
goals:…

Sudheej
- 1,873
- 6
- 30
- 57
0
votes
1 answer
Chainable struct methods that satisfies multiple interfaces?
I have a need to create multiple structs with almost the same fields and methods with the same actions. Instead of doing that I thought, why not create a single struct and use interfaces to limit interactions. It worked great! Now the problem. I…

CodeBreaker
- 488
- 9
- 18
0
votes
2 answers
Cast a json in a properly struct instead of use an interface
i'm struggling to create a data structure for unmarshal the following json:
{
"asks": [
["2.049720", "183.556", 1576323009],
["2.049750", "555.125", 1576323009],
["2.049760", "393.580", 1576323008],
["2.049980",…

alessiosavi
- 2,753
- 2
- 19
- 38
0
votes
1 answer
Programmatically fill golang struct
I have a file with many types of data record which I need to parse into structs.
I'd be grateful to learn of a idiomatic way -- if it exists -- of filling structs by record type. Something like python's namedtuple(*fields) constructor.
package…

rorycl
- 1,344
- 11
- 19
0
votes
1 answer
gob.Register name not registered for interface in another package
I have recently restructured my code so that now under the main package there are two packages: chain and api.
In chain I defined a few structs SomeStruct1, SomeStruct2 and an interface SomeInterface for those structs. The following is what…

3tbraden
- 667
- 1
- 10
- 21
0
votes
3 answers
Does not implement, wrong type for method
Given the following Go code:
package main
type CatToy interface {
Rattle() string
}
type Cat struct {
}
func (cat *Cat) Play(catToy CatToy) {
println("The cat is playing!", catToy.Rattle())
}
type DogToy interface {
Roll()…

Abakada
- 141
- 2
- 9
0
votes
1 answer
trying to implement polymorphism with go interfaces
I'm trying to create a layer on top of a third party library, in this case libchan. Here's an interface I've defined:
type ReceiverStream interface {
Receive(msg interface{}) error
}
type InboundTransport interface {
WaitReceiveChannel()…

dopatraman
- 13,416
- 29
- 90
- 154
0
votes
3 answers
convert interface{} directly to int in Golang, where interface stores a number as string
I got a map[string]interface{} because decoding to JSON; with normal data, interface most be only a number but in type string, like this :
var a interface{}
a="3"
And then all data will be stored into a struct.
type someStruct struct {
ID…

John Balvin Arias
- 2,632
- 3
- 26
- 41
0
votes
1 answer
Golang type assertion / cast to intermediate struct
Given the following types:
type Event interface{}
type ActionResultEvent struct {
Result string
}
type ActionSuccessEvent ActionResultEvent
type ActionFailureEvent ActionResultEvent
type eventHandleFunc func(e Event)
My goal is to have event…

tworabbits
- 1,203
- 12
- 17
0
votes
1 answer
Golang interface & *interface
I am trying to use interfaces more in my codebase for a game server I am writing and understand the high level concept and when one should be used (I think). In my case I am using them to decouple my packages from one another amongst other as well…

John
- 343
- 2
- 5
- 15
0
votes
1 answer
Golang struct pointer invokes interface method
I'm picking up Golang and I've got a problem in traversing a linked list. What I intend to do is visit all nodes of the linked list, and call an interface method from each node.
I've defined an interface
type Sortable interface {
CompareTo(t…

Junxi
- 37
- 1
- 6