Questions tagged [go-interface]

104 questions
1
vote
1 answer

Go interface using `var` keyword?

I am referring to the code in this link: Interfaces in Golang Under the "Type assertions" section, the first piece of code has a line like so in the main function: var val interface {} = "Geeks for Geeks" I did not understand this syntax. Usually,…
KeyShoe
  • 84
  • 7
1
vote
2 answers

Golang panic: interface conversion: interface {} is nil, not string

I have tried to create CA configuration file, certificate and private key, I am using cfssl command in go and try to simulate same command from cfssl gencert -initca ca-csr.json | cfssljson -bare ca Here is my code snipped package main import ( …
Bora Özkan
  • 73
  • 1
  • 1
  • 8
1
vote
1 answer

How to ignore MarshalJSON implementation of a struct (with nested structs)?

Is it possible to ignore a custom MarshalJSON implementation of a struct, and use just standard marshaling function instead? The struct is complex, and has a lot of nested structs, all of which are using custom MarshalJSON, and I would like to…
Igor Chubin
  • 61,765
  • 13
  • 122
  • 144
1
vote
1 answer

How to define interface for methods on struct pointer

For structs, it is possible to define a func that can update struct variables. Is there any way to use those functions in interface? In the following code, I tried to create a minimal example to describe my question. two struct of Rect and Circle…
Amirreza Noori
  • 1,456
  • 1
  • 6
  • 20
1
vote
0 answers

Are there any programming pitfalls of using a map with an empty interface as the KEY

Are there any programming pitfalls of using maps in this manner: type Set struct { theMap map[interface{}]struct{} } StringSet := NewSet("abc", "pqr") IntSet := NewSet(1, 2) DateSet := NewSet(time.Date(2021, 2, 15, 0, 0, 0, 0, time.UTC)) Just…
user15558657
1
vote
1 answer

Using msgp with interfaces and maps in Go

I have a map that uses an interface as the key. The map is defined like this MyMap map[Signature]Packets. The interface is Signature, and there will be two structs A and B that implement this interface. I am also using msgp to serialize these two…
Sebastian
  • 11
  • 2
1
vote
0 answers

Making an AST in Go

I'm writing a parser for a custom language in go. My problem is that each node on the syntax tree is different based on it's type (be it an if or a variableDef or a funcDef) and thus has different values, but I need to have pointers that can point…
1
vote
1 answer

How to cast interface{} to a map in GO

I am new to Go & I am trying to learn how to cast interface{} to a Map. Here is an example of what I am trying to implement. Playground link : https://play.golang.org/p/3jhKlGKO46Z Thanks for the help. package main import ( "fmt" ) func…
1
vote
1 answer

How to mock an http.Handler with Go's moq?

I'm interested in creating an httptest.Server which simply records the requests it is called with. To this end, I'd like to use the github.com/matryer/moq library. To generate a mock for an http.Handler, I copied its definition in a package called…
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
1
vote
1 answer

How to cast nil interface to nil other interface

I have a classic Go nil interface issue. I'm trying to assert an interface{}, which I assign from a nil error, back to an error interface. That sentence is confusing so I have a handy-dandy example: https://play.golang.com/p/Qhv7197oIE_z package…
crunk1
  • 2,560
  • 1
  • 26
  • 32
1
vote
2 answers

What is the difference between the two err value?

I'm trying to understand golang interface, my problem is that why err2.What undefined. Here is a simple code. The output indicates that both err and err2 have same type as *main.MyError, but err2 have no field "What", so there must be some…
Cyanic
  • 9
  • 5
1
vote
2 answers

Can interface type and value be a type that does not implement the interface and its value?

Here is the link to the code and description I was looking at: https://tour.golang.org/methods/11 I change method M of type *T to T, that is changing from a pointer receiver to a value receiver as below. package main import ( "fmt" …
user5574376
  • 371
  • 1
  • 5
  • 12
1
vote
0 answers

How to use and implement empty golang interface { } in C programming

string_operation.go (golang function) //export AddString func AddString(str string, v interface{}) bool { somestrings = append(somestrings, str); xyz.Compile(str, v) fmt.Println("Added!!\n"); } return…
Hemant
  • 11
  • 2
1
vote
2 answers

How to access type methods that are not defined in interface?

I have interface that requires its implementor to have Click method. However, the type FakeTicker that implements this interface also implements Tick method in addition to Click method. package main type Ticker interface{ Click() string…
p-ray
  • 49
  • 6
1
vote
1 answer

How to set an interface{} parameter by reference?

I have a function that has a parameter with the type interface{}. This parameter represents my template data. So on each page it stores different data types (mostly structs). I want to append some data to this parameter's data, but it's an…
Pascut
  • 3,291
  • 6
  • 36
  • 64