I don't expect this should be very difficult, go-wise, but I haven't written much code with generics yet.
I have an application that can read commits from either github or bitbucket, but each run will either be for bitbucket or github, not both. I have to retrieve the commits for a branch a couple of times, so I have a "commitsCache" that is keyed by the project, repo, and branch. All three of those have analogs in github and bitbucket.
For now, I've declared "BitbucketCommit" and "GithubCommit" structs. It's entirely possible I could merge them eventually, but I'll leave them separate for now.
I have this declaration:
commitsCache = make(map[string][]any)
Note the element type of "any", not BitbucketCommit or GIthubCommit. I assume this is correct.
I have both a "get" and "store" method. The following is what I have so far for the "get" method:
func getFromCommitsCache[T any](project string, repo string, branch string) ([]T, bool) {
commitsCacheMutex.RLock()
defer commitsCacheMutex.RUnlock()
if commits, found := commitsCache[project+"/"+repo+"/"+branch]; found {
if v, ok := commits.([]T); ok { // This line.
return v, true
}
}
return nil, false
}
I'm not quite sure what to do here. The line with "this line" has the following compile error:
invalid operation: commits (variable of type []any) is not an interfacecompilerInvalidAssert
I'm currently using Go v1.22.6 , although I could certainly upgrade, if there's a good reason to.