package handler import ( "bufio" "os" "strconv" "github.com/gin-gonic/gin" ) func Index(c *gin.Context) { p, _ := strconv.Atoi(c.Query("p")) if p < 1 { p = 1 } limit := 5 start := (p - 1) * limit list := make([]*Article, 0) sortListPath := "./data/index_sort_list" file, _ := os.Open(sortListPath) defer file.Close() scanner := bufio.NewScanner(file) scanner.Scan() total, _ := strconv.Atoi(scanner.Text()) for i := 0; i < start+limit; i++ { scanner.Scan() if i >= start { id := scanner.Text() article, err := new(Article).GetArticleById(id) if err != nil { continue } list = append(list, article) } } nextPage := 0 if start+limit < total { nextPage = p + 1 } prevPage := 0 if p >= 2 { prevPage = p - 1 } title := os.Getenv("HOME_TITLE") c.HTML(200, "index.html", gin.H{ "list": list, "total": total, "title": title, "page": p, "nextPage": nextPage, "prevPage": prevPage, }) }