网站兼容问题,网站access数据库被攻击不断增大,工作方案的格式及范文,昆山建设工程交易网站看了两天时间的go#xff0c;对于go的编码风格还不是很了解#xff0c;但是了解到go并未有Java那样成体系的编码风格规范#xff0c;所以自己浅尝试了一下#xff0c;风格无对错#xff0c;欢迎交流讨论#xff5e; controller层#xff1a;
package … 看了两天时间的go对于go的编码风格还不是很了解但是了解到go并未有Java那样成体系的编码风格规范所以自己浅尝试了一下风格无对错欢迎交流讨论 controller层
package apiimport (errorsfmtgithub.com/gin-gonic/ginnet/http
)
import awesomeProject/src/servertype pathAndHandler struct {path stringhandler gin.HandlerFunchttpMethod string
}func getApis() []pathAndHandler {return []pathAndHandler{{/, sayHello, http.MethodGet},{/label/v1, sentenceToWord, http.MethodPost},{/label/v2, textToSentence, http.MethodPost},}
}
func instances() *gin.Engine {return server.Init()
}func Register() (server.Server, error) {server : instances()for _, api : range getApis() {switch api.httpMethod {case http.MethodGet:server.GET(api.path, api.handler)case http.MethodPost:server.POST(api.path, api.handler)case http.MethodDelete:server.DELETE(api.path, api.handler)case http.MethodHead:server.HEAD(api.path, api.handler)case http.MethodPut:server.PUT(api.path, api.handler)case http.MethodOptions:server.OPTIONS(api.path, api.handler)case http.MethodPatch:server.PATCH(api.path, api.handler)default:fmt.Println(请求方法不支持)return nil, errors.New(unsupported method)}}return server, nil
}func sentenceToWord(context *gin.Context) {//todo 业务逻辑
}
func textToSentence(context *gin.Context) {}
func sayHello(ctx *gin.Context) {ctx.String(http.StatusOK, hello world)
}main
func main() {server, err : api.Register()if server ! nil {server.Run(:8081)} else {fmt.Println(fail to start server, for reason:/n err.Error())}
}