죄송합니다.이 질문이 약간 기본적인 것이면. Golang 인터페이스를 사용하여 CRUD를보다 동적으로 구현하려고합니다. 모델 category Category
다수 사용할 수구현 한 메소드에서 인터페이스를 반환하는 방법은 무엇입니까?
type Datastore interface {
AllQuery() ([]interface{}, error)
ReadQuery() ([]interface{}, error)
UpdateQuery() ([]interface{}, error)
CreateQuery() ([]interface{}, error)
DestroyQuery() ([]interface{}, error)//Im not sure if the return value implementation is correct
}
, tag Tag
.etc 그것은 응용의 모델을 나타내는 구조체를 나타내는 방법을 구현하고 다음과 같이 난 인터페이스를 구현 하였다.
이 함수이다 : 여기
단순화 된 처리기/제어기 FUNC의 UpdateHandler (c의 handler.context) 오류 {= 새 (models.Post) 복귀 업데이트 (p, c) 가 P}이고 인터페이스func Update(data Datastore,c handler.context) error{
if err := c.Bind(data); err != nil {
log.Error(err)
}
d, err := data.UpdateQuery()
//stuff(err checking .etc)
return c.JSON(fasthttp.StatusOK, d)///the returned value is used here
}
를 구현하고 내가 데이터베이스
func (post Post) UpdateQuery() ([]interface{}, error){
//run query using the
return //I dont know how to structure the return statement
}
에게 쿼리 사용하고있는 방법은3210
위의 인터페이스와이를 구현하는 메소드를 어떻게 구성하여 쿼리 결과를 구현 기능으로 되돌릴 수 있습니까? 질문에 무엇이든 추가하거나 개선해야 할 경우 즉시 알려주십시오. 감사합니다.
이 유사 설계 경우 CRUD 인터페이스는 더 좋을 수 [this one] (https://godoc.org/github.com/sauerbraten/crudapi#Storage) –
정말 예를 생각해보아야합니다. 그리고 제발 : 빈 인터페이스를 제거하십시오. 무엇을하려고하든,'interface {}'로 끝내면 잘못 될 것입니다. – Volker