go接口方法重载

go接口方法重载

May 8, 2024
Go

源码 #

package main

import "fmt"

type Log interface {
	Error()
}

type LocalLog struct {
	Level   string `json:"level"`
	Key     string `json:"key"`
	Msg     string `json:"msg"`
	TraceId string `json:"trace_id"`
}

type Atta struct {
	Level   string `json:"level"`
	Key     string `json:"key"`
	Msg     string `json:"msg"`
	TraceId string `json:"trace_id"`
	AttaId  string `json:"atta_id"`
}

func CommError(obj Log) {
	obj.Error()
}

func (l *LocalLog) Error() {
	fmt.Println(l.Level, l.TraceId)
}

func (a *Atta) Error() {
	fmt.Println(a.Level, a.TraceId, a.AttaId)
}

func main() {

	l := LocalLog{
		Level:   "error",
		Key:     "local",
		Msg:     "本地日志消息",
		TraceId: "76482-32r872t87d-21e787e8",
	}

	l.Error()
	a := Atta{
		Level:   "error",
		Key:     "atta",
		Msg:     "atta日志消息",
		TraceId: "67235r4-3hh232j0-32-384hrbj",
		AttaId:  "72364028640326402",
	}
	a.Error()

	CommError(&l)
	CommError(&a)
}