Monday, May 20, 2024
HomeGolangAssist with compile error, please - Getting Assist

Assist with compile error, please – Getting Assist


I’m pretty new to Golang. I’m making an attempt to implement what, within the C, language could be a tagged union. I discovered some instance code on: Go and Algebraic Information Varieties – Eli Bendersky’s web site

I feel I perceive the instance, and am making an attempt to emulate it in my code. Here’s a excerpt of my full program that will get an error that I don’t perceive. All of it appears okay to me.


package deal foremost

sort Obj interface {
    isObj()
}

sort Tspecial struct {
    subtype int
}

func (Tspecial) isObj() {}

func make_special(subtype int) *Obj {
	n := Tspecial{subtype}
	return &n
}

func foremost() {
    // dot := make_special(1)
}

It will get the next error with go model 1.19.1:

./take a look at.go:15:9: can not use &n (worth of sort *Tspecial) as sort *Obj in return assertion:
*Tspecial doesn’t implement *Obj (sort *Obj is pointer to interface, not interface)

I feel I’ve issues such that Tspecial implement Obj, so why wouldn’t the deal with of a Tspecial be a *Obj ?

In any case, can somebody please inform me my error. I’m positive there’s a solution to do what I’m making an attempt to do.

package deal foremost

import "fmt"

sort Obj interface {
	isObj()
}

sort Tspecial struct {
	subtype int
}

func (ts *Tspecial) isObj() {}

func make_special(subtype int) Obj {
	n := Tspecial{subtype}
	return &n
}

func foremost() {
	dot := make_special(1)
	fmt.Println(dot)
}

Nicely, your suggestion compiles, bt I can’t perceive why it does. It appears very incorrect to me, as a result of the operate make_special says it returns an Obj, not some extent to an Obj, and your code returns “&n”, the deal with of n, which ought to be a pointer to an Obj.

Can somebody please clarify, slowly, or level me to documentation, and assist me perceive this. I have to be lacking one thing elementary.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments