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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题