Using go to emulate an interface implementation class in Java

Source: Internet
Author: User
For the project, the same process business refactoring, using go to simply simulate the Java interface, the implementation of the way the class is refactored.
Share a simple example:

package main//模拟java中的接口,实现类import "fmt"// 声明一个接口,type workthread interface {    do(name string)}// 传入的wt的子类不同,do方法执行的逻辑也不同func Handle(wt workthread, name string) {    fmt.Println("--------执行do方法前的逻辑------")    wt.do(name)    fmt.Println("--------执行do方法后的逻辑------")}type SparkNode struct {    ClusterName string}func (sn *SparkNode) do(name string) {    fmt.Println("----hello, I‘m spark node", "\t", sn.ClusterName)}func (sn *KafkaNode) do(name string) {    fmt.Println("----hello, I‘m kafka node\t", sn.ClusterName)}type KafkaNode struct {    ClusterName string}func main() {    sparkNode := &SparkNode{        ClusterName: "k8s",    }    kafkaNode := &KafkaNode{        ClusterName: "kafka-node",    }    Handle(sparkNode, "111")    Handle(kafkaNode, "222")}

Using go to emulate an interface implementation class in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.