ZhangZhihui's Blog  

fake.go

复制代码
package fake

import "fmt"

type ExampleStruct struct {
    Item1 string
    Item2 string
    Item3 string
}

func (e ExampleStruct) LogLine() {
    fmt.Printf("%+v\n", e)
}
复制代码

 

main.go

复制代码
package main

import (
    "github.com/xxx/fake"
)

func main() {
    a := fake.ExampleStruct{Item1: "a"}
    PrintExample(a)
}
func PrintExample(e fake.ExampleStruct) {
    e.LogLine()
}
复制代码

 

new_main.go

复制代码
package main

import (
    "github.com/xxx/fake"
)

func main() {
    a := fake.ExampleStruct{Item1: "a"}
    PrintExample(a)
}

type ExampleInterface interface {
    LogLine()
}

func PrintExample(e ExampleInterface) {
    e.LogLine()
}
复制代码

 

anotherfake.go

复制代码
package anotherfake

import "fmt"

type AnotherExampleStruct struct{}

func (e AnotherExampleStruct) LogLine() {
    fmt.Printf("This is from anotherfake package :: %+v\n", e)
}
复制代码

 

new_main_4_anotherfake.go

复制代码
package main

import (
    fake "github.com/xxx/anotherfake"
)

func main() {
    a := fake.AnotherExampleStruct{}
    PrintExample(a)
}

type ExampleInterface interface {
    LogLine()
}

func PrintExample(e ExampleInterface) {
    e.LogLine()
}
复制代码

 

Notice that in the modified main Golang module, the only that is being altered is the struct that is being defined and instantiated to be passed to the rest of the code in the module. Other than that, there is little or no other change that is kind of required.
posted on   ZhangZhihuiAAA  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
 
点击右上角即可分享
微信分享提示