Reuse function with return type always equal to the struct that is calling that function in golang

huangapple 未分类评论73阅读模式
英文:

Reuse function with return type always equal to the struct that is calling that function in golang

问题

我正在尝试在Golang中做一些在Java中使用Type可以轻松完成的事情。例如:

public class MyClass<Type extends MyClass<Type>> {
    public Type myMethod() {
        // Do something
        return (Type) this;
    }
}


public class Test1 extends MyClass<Test1> {
    public String A;
    
    Test1(String A) {
        this.A = A;
    }
}

public class Test2 extends MyClass<Test2> {
    public String B;

    Test2(String B) {
        this.B = B;
    }
}

因此,在主函数中,我可以这样做:

Test1 t1 = new Test1("hello");
Test2 t2 = new Test2("world");
System.out.println(t1.myMethod().A); // 输出 "hello"
System.out.println(t2.myMethod().B); // 输出 "world"

我正在尝试弄清楚在Go中是否有一种类似的方法。也许可以这样做:

type test1 struct {
	A string
}

type test2 struct {
	B string
}


func (val Type) MyMethod() Type {
	// Do something
	return val
}


func main() {
	t1 := test1{A: "hello"}
	t2 := test2{B: "world"}
	fmt.Println(t1.MyMethod().A) // 输出 "hello"
	fmt.Println(t2.MyMethod().B) // 输出 "world"
}
英文:

I'm trying to do in Golang something that would be easily done in Java with Type. For example:

public class MyClass&lt;Type extends MyClass&lt;Type&gt;&gt; {
    public Type myMethod() {
        // Do something
        return (Type) this;
    }
}


public class Test1 extends MyClass&lt;Test1&gt; {
    public String A;
    
    Test1(String A) {
        this.A = A;
    }
}

public class Test2 extends MyClass&lt;Test2&gt; {
    public String B;

    Test2(String B) {
        this.B = B;
    }
}

So, in main I can do

Test1 t1 = new Test1(&quot;hello&quot;);
Test2 t2 = new Test2(&quot;world&quot;);
System.out.println(t1.myMethod().A); // Prints &quot;hello&quot;
System.out.println(t2.myMethod().B); // Prints &quot;world&quot;

I'm trying to figure out if there is a way to do somethig similar in Go.
Maybe something like:

type test1 struct {
	A string
}

type test2 struct {
	B string
}


func (val Type) MyMethod() Type {
	// Do something
	return val
}


func main() {
	t1 := test1{A: &quot;hello&quot;}
	t2 := test2{B: &quot;world&quot;}
	fmt.Println(t1.MyMethod().A) // Prints &quot;hello&quot;
	fmt.Println(t2.MyMethod().B) // Prints &quot;world&quot;
}

答案1

得分: 0

感谢JimB,我找到了一种方法。

从Go 1.18开始,支持泛型类型。

因此,在Go中,Java的MyClass将类似于以下代码:

type MyGeneric[T any] struct {
	self *T
}

func (val MyGeneric[T]) MyMethod() *T {
	// 做一些操作
	return val.self
}

Java类Test1将是:

type test1 struct {
	MyGeneric[test1]
	A string
}

func MakeTest1(A string) test1 {
	answer := test1{A: A}
	answer.self = &answer
	return answer
}

类似地,Java类Test2将是:

type test2 struct {
	MyGeneric[test2]
	B string
}

func MakeTest2(B string) test2 {
	answer := test2{B: B}
	answer.self = &answer
	return answer
}

所以,主函数如我们所想:

t1 := MakeTest1("hello")
t2 := MakeTest2("world")
fmt.Println(t1.MyMethod().A) // 输出 "hello"
fmt.Println(t2.MyMethod().B) // 输出 "world"
英文:

Thanks to JimB I found a way.

From Go 1.18 Generic Types are sypported.

So the Java MyClass will look something like this in Go:

type MyGeneric[T any] struct {
	self *T
}

func (val MyGeneric[T]) MyMethod() *T {
	// Do something
	return val.self
}

The Java class Test1 will be:

type test1 struct {
	MyGeneric[test1]
	A string
}

func MakeTest1(A string) test1 {
	answer := test1{A: A}
	answer.self = &amp;answer
	return answer
}

And similarly the Java class Test2:

type test2 struct {
	MyGeneric[test2]
	B string
}

func MakeTest2(B string) test2 {
	answer := test2{B: B}
	answer.self = &amp;answer
	return answer
}

So the main is as we wanted

t1 := MakeTest1(&quot;hello&quot;)
t2 := MakeTest2(&quot;world&quot;)
fmt.Println(t1.MyMethod().A) // Prints &quot;hello&quot;
fmt.Println(t2.MyMethod().B) // Prints &quot;world&quot;

huangapple
  • 本文由 发表于 2022年7月18日 22:03:57
  • 转载请务必保留本文链接:https://java.coder-hub.com/73023641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定