嗨,我想以本地化的人类可读方式(例如 9 天 3 小时)表示 luxon 间隔。
从现在开始,我已经实现了这一点。使用此代码:
DateTime.fromISO(value).toRelative({ locale: "es" });
Run Code Online (Sandbox Code Playgroud)
但是我不能使用 Interval o Duration 对象来实现相同的效果。
这就完成了工作。但并不是真正的本地化。
const start = DateTime.fromSQL("2020-06-19 11:14:00");
const finish = DateTime.fromSQL("2020-06-21 13:11:00");
const {days, hours, minutes} = Interval
.fromDateTimes(start, finish, {locale: "es"})
.toDuration(["days", "hours", "minutes"]).values;
console.log(
`${days ? days + " días " : ""} ${hours ? hours + " horas" : ""} ${
minutes ? minutes + " minutos." : ""
}`
);
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 go-chi 和 Gorm 构建 REST API。
我不确定应该如何将 Gorm DB 实例传递给路由处理程序。
或者,如果我应该为每个处理程序创建一个实例,这对我来说听起来不太合适。
我应该使用中间件、依赖注入还是其他?这里推荐什么模式?
package main
import (
"encoding/json"
"fmt"
"github.com/go-chi/chi/v5"
"log"
"net/http"
"os"
"time"
)
func main() {
r := chi.NewRouter()
r.Get("/", indexHandler)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
db := Connect()
migrations(db)
logStartServer(port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), r))
}
func logStartServer(port string) {
log.Printf("Listening on port %s", port)
log.Printf("Open http://localhost:%s in the browser", port)
}
func indexHandler(w http.ResponseWriter, r *http.Request) …Run Code Online (Sandbox Code Playgroud)