Questions tagged [go-interface]

104 questions
3
votes
2 answers

Golang plugin type assertion

I'm writing a simple app which loads plugin in a predefined format. Example plugin is the following: package main import ( "errors" "fmt" "strings" ) var ( ok bool InvConfig = errors.New("invalid config") ) type…
pomo_mondreganto
  • 2,028
  • 2
  • 28
  • 56
3
votes
1 answer

List all types implementing an interface in Go

I'm new to Go, and I'd like to know if there's a way in Goland or w/ a CLI tool to list all the types that implement a given interface. I found a tool called guru that can list all interfaces implemented by a given type, but I haven't been able to…
jeremie
  • 971
  • 9
  • 19
3
votes
1 answer

golang get values from interface{}

I am iterating through the results returned from a couchDB.View and extracting the Key. for _, row := range rows { fmt.Printf("%v, %T\n", row.Key, row.Key) } The result of this is: [nh001 mgr], []interface {} [nh002 nh], []interface…
davidbilla
  • 2,120
  • 1
  • 15
  • 26
3
votes
1 answer

Assert interface to its type

I can't gracefully get pixels of an image as array in general case. f, err := os.Open(imgPath) check(err) defer f.Close() img, _, err := image.Decode(bufio.NewReader(f)) check(err) pixels, err := getPixels(img) check(err) // Logic with pixels. Now…
Andrey
  • 1,495
  • 17
  • 14
3
votes
1 answer

Confusion with pointer, slices and interface{} in function arguments in go

I've been reading about how Go passes arguments to functions via pointer vs. value. I've been reading about the interface type. And I've been tampering with the reflect package. But clearly, I still don't understand how it all works because of…
John
  • 32,403
  • 80
  • 251
  • 422
3
votes
1 answer

GoLang - Passing nil to function that accepts an interface

I stumbled accross something that threw me for a loop in golang recently. package main import ( "net/http" "bytes" "fmt" "io/ioutil" ) func createRequest(method, uri string, data *bytes.Buffer) string { req, err :=…
soluwalana
  • 68
  • 7
2
votes
2 answers

How do I work out what type of error my error is in go?

I'm not sure exactly how to phrase this question, and I've seen others ask similar but not really come up with answers (which tells me I'm asking the wrong question, but I'm not sure how else to approach this). I'm trying to learn some basic Go, and…
Dan W
  • 37
  • 1
  • 3
2
votes
0 answers

Go abstractions and errors

Let's say I need an abstraction for some type that has a method Do, which returns an error package main import "myproject/complex" type Doer interface { Do(i int) error } func main() { var doer Doer doer = complex.Doer{} i := 1 //…
savolro
  • 39
  • 2
2
votes
2 answers

Golang with GORM. How to correctly pass model type into method?

I have next GORM model package entity import ( "github.com/jinzhu/gorm" ) type InterfaceEntity interface { } type User struct { InterfaceEntity gorm.Model Name string } I try to pass GORM entity type into base crud repository. My…
Nick
  • 9,735
  • 7
  • 59
  • 89
2
votes
2 answers

Is it necessary to use a type assertion to access the values of a type returned by interface?

When I have a function that returns an interface type, the returned value doesn't work as I would expect. That is, it acts strictly as the defined interface, and to access the methods and values not defined in the interface, I have to do a type…
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
2
votes
2 answers

Can I generically specifiy a Method for a Go-Lang Struct?

I have been reading over the go-lang interface doc ; however it is still not clear to me if it is possible to achieve what I'd like type A struct { ID SomeSpecialType } type B struct { ID SomeSpecialType } func (a A) IDHexString() string…
SMTF
  • 705
  • 10
  • 16
2
votes
1 answer

Object Factory in golang

I am a new to golang. I need to design a function to create object of differing types based on input. But I failed to figure out how to design the interface. Here comes my code: package main import ( "fmt" ) type AA struct{ name…
RobinMin
  • 373
  • 1
  • 4
  • 12
2
votes
2 answers

Understanding Interface

I am a newbie in Go lang , I was trying to understand Go Interface by writing a simple piece of code . I am getting an error , as I am not able to understand the correct way of referring to a Interface method kindly tell me where I am going…
Worlock
  • 406
  • 1
  • 4
  • 15
1
vote
0 answers

Implementing grpc in gnark v0.8.1, how to convert Proof, Verification Key & Public Witness to go-native type?

In gnark v0.8.1, We first need to convert those 3 to go-native i.e. we convert them to []byte. I tried implementing Serialize() and DeSerialize(), but I am facing errors. Issue: groth16.Proof is an interface and its implementation depends upon…
1
vote
0 answers

`gopls` disagrees with `go test` on interface implementation

Consider the following code: package ifaces import ( "fmt" "testing" ) type IFace1 interface { Do1() } type IFace2 interface { Do2() } type Inner struct { } func (i *Inner) Do2() { …
caxcaxcoatl
  • 8,472
  • 1
  • 13
  • 21