duan19913 2018-10-26 05:07
浏览 29
已采纳

Golang进口周期挑战

I am having an issue with a cyclic dependency (import cycle not allowed) in my go code and not really sure the best way to resolve it. I think my lack of understanding of golang interfaces might be impacting my ability to see the way forward.

My Issue - I have Two Packages:

  • Event - Event is mostly a "parent" and will make many calls into the workout package
  • Workout - In one case I need to call the Event Package and this creates a cyclic dependency because Event already consumes Workout

What is the best way to allow Workout to call a function (not a method on an object) in the Event domain?

Below is my simplified code to help provide some context

//workout package
//This is a private function on the workout package that is 
//attempting to call a public function on the Event Package
func findWorkoutAssociatedToActivityTcx(txcObject *DataTcx) *EventWorkout{
    return event.GetEventByDate(txcObject.ActivityDate, "", "")
}

Is it possible to create an interface into that function? I don't fully understand how I would do that. Thank you very much.

//UPDATES - Code on attempting to setup an interface

//WorkoutPackage
//workout package
//This is a private function on the workout package that is
//attempting to call a public function on the Event Package
func findWorkoutAssociatedToActivityTcx(txcObject *DataTcx, userID, transactionID string) *Workout {
    //return event.GetEventByDate(txcObject.ActivityDate, "", "")
    MyEventFinder.GetEventByDate(txcObject.ActivityDate, userID, transactionID)
    return nil
}

var MyEventFinder EventFinder

type EventFinder interface {
    GetEventByDate(time.Time, string, string) (*sharedstructs.ListOfEvents, error)
}

//Event Package

type eventProvider struct{}

func (e eventProvider) GetEventByDate(date time.Time, userID, transactionID string) (*sharedstructs.ListOfEvents, error) {
        redFalconLogger.LogDebug("event.GetEventByDate: ", transactionID)
        if date.IsZero() || userID == "" {
            return nil, sharedstructs.InvalidData{Msg: "Invalid date or userID"}
        }
        //Create the query params
        queryParamArray, queryParamCreationError := createQueryParamForQueryByDate(&date, &userID)
        if queryParamCreationError != nil {
            return nil, queryParamCreationError
        }
        //perform the query - pass empty orderBy because I don't care
        queryResults, queryError := queryForEvent(*queryParamArray, "")
        if queryError != nil {
            switch queryError.(type) {
            case firestorehelper.UnqueryableCollection:
                return nil, sharedstructs.Forbidden{Msg: "operation is forbidden, probably due to malformed query"}
            default:
                return nil, sharedstructs.InternalServerError{Msg: "something went wrong in the query"}
            }
        }
        return queryResults, nil
    }
  • 写回答

1条回答 默认 最新

  • dongmuzhan4705 2018-10-26 05:21
    关注

    You are spot on that you can do this with an interface. In the event package you need to give the GetEventByDate a receiver struct:

    type eventProvider struct {}
    func (e eventProvider) GetEventByDate(t time.Time, a, b string) *workout.EventWorkout{...}
    

    Then in the workout package:

    type EventFinder interface {
        GetEventByDate(time.Time, string, string) *EventWorkout
    }
    

    You can then pass an eventProvider instance into the workout package and use that just via the EventFinder interface that has no compile time dependency on the event package. You don't show how you call into the workout package but this could either be as a parameter to a method call or setting it when constructing a struct in the workout package.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)