Questions tagged [go-interface]
104 questions
-1
votes
1 answer
How to convert the value inside an interface into a map in golang?
I am a complete newbie to GO.
My objective is to get the data inside item.Data and put them into a map so that I can access them as a key value pair.
As far as I understand an empty interface is as same as any type in Typescript.
Can you please…

Athul Muralidharan
- 693
- 1
- 10
- 18
-1
votes
1 answer
How to achieve the functionality described below
I need to do something like this:
// I have an interface with these two methods defined
type mapper interface {
ReadMapper(interface{}) interface{}
WriteMapper(interface{}) interface{}
}
then I need to assign ReadMapper and WriteMapper some…

ray an
- 1,132
- 3
- 17
- 42
-1
votes
1 answer
Golang Access Data Elements of Common Struct
I will have an application with many messages adhering to a Req/Rsp design. Implemented below is a single Req/Rsp for the message Foo. A Req will adhere to a common interface for processing the message and return a response. All Rsp messages will…

pyInTheSky
- 1,459
- 1
- 9
- 24
-1
votes
1 answer
In Go, how to check that an interface implements all of a struct's exported methods?
I'm working on a Go library in which I've used the interfacer tool (https://github.com/rjeczalik/interfaces) to create an interface from a struct, and subsequently ran moq (https://github.com/matryer/moq) to generate a mock object for that…

Kurt Peek
- 52,165
- 91
- 301
- 526
-1
votes
1 answer
How to mock structs with interdependent interface methods
I am having trouble writing unit tests in Go for a rather common use-case/pattern.
Imagine, if you will, something like this:
package main
type Resource struct {
name string
}
type ResourceManager interface {
GetResource(id string)…

Karthik
- 740
- 5
- 11
-1
votes
1 answer
How to pass interface to method that have lot of arguments
I had written a lazy code to demonstrate the issue I am having to implement interfaces. I have methods M1, M2 that take struct X as an argument and have a type of struct Y. I wanted all these methods to be implemented by a single interface I. The…

supa_command
- 119
- 2
- 7
-1
votes
2 answers
Using interfaces correctly when creating structs
I'm trying to write a small program in which I have a few packages, each with a struct that implements an interface. The idea is that based on user input, I can choose what package to use to build a particular struct and then call a function on it…

Clicquot the Dog
- 530
- 2
- 6
- 19
-1
votes
2 answers
Underlying pointer type from interface value
How do I get the underlying pointer type from an interface?
package main
import (
"fmt"
)
type Car interface {
Drive() string
}
type MyCar struct {
name string
}
func (MyCar) Drive ( ) string {
return "rum rum"
}
func main()…

ealfonso
- 6,622
- 5
- 39
- 67
-2
votes
1 answer
When and why to return an interface in Golang?
Example code:
type IClient interface {
UploadFile(sourcePath, host string) error
CopyFile(sourcePath, destPath string) error
DeleteFile(sourcePath, option string) error
GetChecksum(sourcePath string) (*string, error)
…

Lee Dan
- 33
- 4
-2
votes
1 answer
Range over element of interface implementation
Let's supposed I've got the following interface and two structs that implement it:
type Tmp interface {
MyTmp() string
}
type MyStructA struct {
ArrayOfItems []int
}
func (a MyStructA) MyTmp() string {
return "Hello World"
}
type…

HerbertRedford
- 230
- 4
- 14
-2
votes
2 answers
Struct embedding of interfaces, panic: runtime error
I'm trying an example related to struct embedding of interfaces
// https://talks.golang.org/2014/go4java.slide#52
// Struct embedding of interfaces
// https://play.golang.org/p/SYiZ7M1OEhU
package main
import (
"bytes"
"fmt"
…

JuanPablo
- 23,792
- 39
- 118
- 164
-2
votes
1 answer
Golang abstract function that gets data from db and fills array
I want to create an abstract function, that gets data from DB and fills array by this data. Types of array can be different. And I want to do it without reflect, due to performance issues.
I just want to call everywhere some function like…

Vincento Law
- 1
- 1
- 4
-3
votes
1 answer
Rule for GoLang Interface name and its number of methods
Ones I had a discussion at work about correlation between interface name and number its methods.
In particular, there is an unwritten rule about an interface with postfix notation ending with er in its name.
The rule says such an interface should…

CodeGroover
- 2,157
- 19
- 25
-4
votes
1 answer
Split array of objects with some group of keys
I have an example of json as below:
{"key1": "val1", "key2": "val2", "key3": [{"k1": v1"}, {"k2": "v2"}]}
Now I need to split it into two objects:
{"key1": "val1", "key2": "val2", "key3": {"k1": v1"}} and
{"key1": "val1", "key2": "val2", "key3":…

u_peerless
- 644
- 2
- 9
- 23