Return empty list instead of null
问题 I want to change my current function to return empty JSON list, currently it returns nil . This is my current code: func (s *Service) projectsGet(c *gin.Context) { var projects []*models.Project user := getUser(c) pag := models.NewPagination(c) ps, err := s.db.ProjectsGet(user.ID, &pag) if err != nil { apiError(c, http.StatusInternalServerError, err) return } projects = ps c.JSON(http.StatusOK, projects) } I want it to return [] , how I can do it? 回答1: A nil slice encodes to a null JSON