Questions tagged [go-interface]

104 questions
1
vote
1 answer

pointer receiver vs value receiver

Situation: I've learned about pointer receivers and value receivers. From what I understand: if you want to modify the object itself, you need to use a pointer receiver. I was reading more about interfaces in the go documentation and found this…
phouse512
  • 650
  • 4
  • 15
  • 27
1
vote
1 answer

Unmarshaling a JSON integer to an empty interface results in wrong type assertion

I have this code. I expect the interface to be type asserted to int. However, the type of the interface is float64 instead. Can anyone explain why this is happening? And what are the best ways to circumvent it. package main import ( "fmt" …
Keeto
  • 4,074
  • 9
  • 35
  • 58
1
vote
1 answer

Is it possible to pass interface obj to interface{} type?

I'm new to golang and I want to implement an overloaded method something similar to C++ overloading, and my code looks something like this: type someStruct struct { val int some string } type object interface { toByte() } //…
Ronin Goda
  • 234
  • 5
  • 13
1
vote
1 answer

Is it possible to store a Go type

I've got a handful of interfaces, and n number of structs that arbitrarily implement these interfaces. I'd like to keep an array of types and be able to run a loop over them to see which ones are implemented. Is it possible to store a type like…
estobbart
  • 1,137
  • 12
  • 28
1
vote
1 answer

Create a struct from a byte array

I use the json.Marshal interface to accept a map[string]interface{} and convert it to a []byte (is this a byte array?) data, _ := json.Marshal(value) log.Printf("%s\n", data) I get this output {"email_address":"joe@me.com","street_address":"123…
7hacker
  • 1,928
  • 3
  • 19
  • 32
1
vote
1 answer

Go obtain pointer to struct from interface?

Given an interface, how do I obtain a pointer to the underlying value? My naive attempt was to use a type assertion like this: var mytypeptr *MyType = myinterface.(*MyType) But I get: interface conversion: MyInterface is MyType, not *MyType
ealfonso
  • 6,622
  • 5
  • 39
  • 67
0
votes
0 answers

Appending to slice in map which is type of map[any]any

I want to append to slice in map which is defined as map[any]any I am having type conversion issues and don't know how to treat result[index][ABSPath] as slice so I can append data to it Here is the example code: package main import ( "fmt" …
John Doe
  • 17
  • 1
0
votes
0 answers

Calling functions with unknown number of arguments

I have the following struct: // fninfo holds a function and its arity. type fninfo struct { arity int // number of arguments that fn takes fn any // fn must take string arguments and return one string } and a map of it: var fnmap =…
ban_javascript
  • 339
  • 1
  • 4
  • 16
0
votes
0 answers

Go - Pass callback function to child (imported) package

I have a go program, MyPackage, and a logging package within this, MyPackage/logs. We have a function func Cleanup(params CleanupParams) written in MyPackage that we want to use as a callback in case of any panic within our log function implemented…
MattyAB
  • 365
  • 2
  • 9
  • 19
0
votes
1 answer

golang interface and why output is "bad error"?

I try to define "Error" method to type "T", but why value changed?? type T int func (t T) Error() string { return "bad error" } func main() { var v interface{} = T(5) fmt.Println(v) //output: bad error, not 5 } How to explain this…
linfun486
  • 11
  • 4
0
votes
1 answer

Why does Go allow a struct to implement an unexported interface present in a different package?

I wrote a sample code to understand how the unexported interface works. In the below example, I have declared the unexported repoInterface in the service package. TestRepo struct in the repo package implements the unexported repoInterface without…
0
votes
2 answers

Go interfaces: is not net.Conn a io.ReadWriteCloser?

I have a question about the uses of interfaces in go. I am still learning the language, so please forgive me if it is a stupid question. I am playing with a little application that accept and handle TCP connections. Because I want to test the…
Francesco
  • 11
  • 2
0
votes
1 answer

using maps with any value type as function parameter?

I am trying to create a function which takes in a map as a parameter, where the map uses string keys but the values can be ANY type. How do I make this work in go? I tried using map[string]interface{} as the parameter type to the function but that…
Atif Ali
  • 2,186
  • 2
  • 12
  • 23
0
votes
1 answer

Sharing method implementations between different structs

Say we have 2 structs sharing a property with the same name and purpose, but of different size: type ( L16 struct { Length uint16 } L32 struct { Length uint32 } ) The goal is to make those structs have a GetLength…
Greendrake
  • 3,657
  • 2
  • 18
  • 24
0
votes
1 answer

Go interfaces and Rust traits, and porting a function from Go to Rust

I have this piece of go code - type Server struct { enforcerMap map[int]*casbin.Enforcer adapterMap map[int]persist.Adapter } func NewServer() *Server { s := Server{} s.enforcerMap = map[int]*casbin.Enforcer{} s.adapterMap =…
eth_sign
  • 63
  • 7