新手, 用 github.com/labstack/echo 写了一个api, 里面调用mongodb获取一些数据
我调用一次api内存就增加一点点, 感觉内存一直在增加就没有减少过, 等了很久都没有减少过,go不是说自动回收资源嘛,怎么只有增加没有减少, 望大侠指点下可能是哪里出了问题, 大概代码如下:
```go
package api
import (
"etc"
"github.com/labstack/echo"
)
//ApiServer 实体
type ApiServer struct {
echo *echo.Echo
}
//NewApiServer 创建实例,加载配制文件
func NewApiServer() (*ApiServer, error) {
err := etc.New("etc/serverconfig.yml")
if err != nil {
return nil, err
}
e := echo.New()
apiserver := &ApiServer{
echo:e,
}
apiserver.InitRouter()
return apiserver, nil
}
//StartUp 启动实例
func (s *ApiServer) StartUp() error {
return s.echo.Start(etc.Config.ApiServer.Bind)
}
```
```go
package api
import "ctrl"
func (service *ApiServer) InitRouter() {
//get person
service.echo.GET("/api/persons/:person", ctrl.PersonCtr.GetPerson)
//post person
service.echo.POST("/api/persons", ctrl.PersonCtr.SavePerson)
}
```
```go
package ctrl
import (
"strconv"
"strings"
"net/http"
"github.com/labstack/echo"
"ctrl/request"
"ctrl/response"
//"dataaccess"
//"fmt"
)
var PersonCtr = &personController{}
//personController Person控制器
type personController struct {
}
//getPerson 获取单个用户
func (s *personController) GetPerson(e echo.Context) error {
result := &response.Result {Code:0}
person := strings.TrimSpace(e.Param("person"))
personId, err := strconv.Atoi(person)
if len(person) == 0 || err != nil {
result.Message = "person isn't empty"
return e.JSON(http.StatusBadRequest, result)
}
//temp := GetPerson(personId) 调用数据层的接口
temp := "PersonCtroller information: " + strconv.Itoa(personId)
result.Code = 1
result.Message = "success"
result.Data = temp
/*
if data, errdata := dataaccess.GetPersonLog(); errdata != nil {
fmt.Println(errdata)
} else {
result.Data = data
}
*/
return e.JSON(http.StatusOK, result)
}
```
result := &response.Result {Code:0}
改成
result := response.Result {Code:0}
再测试一下看看
#10
更多评论