Please help me to understand if I use the interface correctly in the code below.

package controllers import ( "../models" ) func GetShedule() { s := models.Shedule{} models.Get(&s) } 

_

 package models import "fmt" type Shedule struct { Specname, Fio, Cab, VP1, VP2, Dayw string } type IGetShedule interface { Get() } func (s *Shedule) Get() { s.Specname = "specname" s.Fio = "fio" s.Cab = "cab" s.VP1 = "vp1" s.VP2 = "vp2" s.Dayw = "dayw" } func Get(s IGetShedule) { s.Get() fmt.Println(s) } 

I deliberately do not use error handling in order not to complicate this code. Or how would you write it. (this is part of the REST service, the model is called from controllers)

one more option

 package controllers import ( "../models" "fmt" ) func Shedule() { s := models.Shedule{} models.SheduleFunc(&s, "get") fmt.Println(s) } 

__

 package models type Shedule struct { Specname, Fio, Cab, VP1, VP2, Dayw string } type IGetShedule interface { Get() Put() } func (s *Shedule) Get() { s.Specname = "specnget" s.Fio = "get" s.Cab = "get" s.VP1 = "get" s.VP2 = "get" s.Dayw = "dget" } func (s *Shedule) Put() { s.Specname = "put" s.Fio = "put" s.Cab = "put" s.VP1 = "put" s.VP2 = "put" s.Dayw = "dput" } func SheduleFunc(s IGetShedule, param string) { switch param { case "get": s.Get() case "put": s.Put() } } 

    2 answers 2

    You just want to practice creating interfaces and wondering where to stick it?). As far as I know, it is customary to create an interface in the package that uses it. Those. your models package describes data types and their methods, it offers to interact with these types through the described methods, so it does not need interfaces (there are already methods). And the controllers package wants to use these types and decides how to do it - through existing methods, or create an interface for convenience. In your example, the creation of an interface is really redundant, since you can freely call methods of type Shedule . But if a new type appears, say, Shedule2 , then it is already possible to create an interface in order to consistently interact with both types of data.

     package models type Shedule struct { Specname, Fio, Cab, VP1, VP2, Dayw string } func (s *Shedule) Get() string { // do smth. return fmt.Srintf("%+v", s) } func (s *Shedule) Put() { // do smth. } type Shedule2 struct { Specname, Fio, Cab, VP1, VP2, Dayw string } func (s *Shedule2) Get() string { // do smth. return fmt.Srintf("%+v", s) } func (s *Shedule2) Put() { // do smth. } 

    -

     package controllers type IGetShedule interface { Get() string Put() } func Shedule(s IGetShedule) { result = s.Get fmt.Println(result) } 

    -

     package main import "controllers" import "models" func main() { s := models.Shedule{} controllers.Shedule(s) // Print result s2 := models.Shedule2{} controllers.Shedule(s2) // Print result } 

    That is, we can now transfer both the Shedule type and Shedule type to the Shedule function Shedule(s IGetShedule) . There is no need to write two identical functions for each type.

    • Yes, it is to practice ... thanks for the example, while I understood myself I came to the same example. - Evgeny Gusev

    Interfaces are needed only if there are several specific types with which it is necessary to work uniformly, summarizing their behavior (methods). If you plan to transfer only instances of models. Schedule (and judging by the interface name, this is the case), then an interface with one single type implements it will be an absolutely unnecessary abstraction. Most likely, the models. Get the interface instead of the interface should be * Shedule.

    • Added a second example, is this also redundant? And could you show with my example how the interface works with several types - Evgeny Gusev