类似的问题在这里得到了回答,但我不认为它解决了我的问题.
假设你有以下结构:
type User struct {
Username string
Password []byte
Email string
...
}
Run Code Online (Sandbox Code Playgroud)
此外,URL的结构如下:example.com/en/users,其中"en"是一个URL参数,将被传递到模板中,如下所示:
renderer.HTML(w, http.StatusOK, "users/index", map[string]interface{}{
"lang": chi.URLParam(r, "lang"),
"users": users})
Run Code Online (Sandbox Code Playgroud)
在HTML模板中,我有以下内容:
{{ range .users }}
<form action="/{{ .lang }}/users" method="POST">
<input type="text" name="Username" value="{{ .Username }}">
<input type="text" name="Email" value="{{ .Email }}">
</form>
{{ end }}
Run Code Online (Sandbox Code Playgroud)
现在,问题是因为{{.lang}}不是User结构的一部分,所以我得到了错误..那么如何在{{range .users}}中访问{{.lang}}?
有没有人设法弄清楚如何让 Vue.jshistory mode与 GitHub 或 GitLab Pages一起工作?
它适用于hash mode,但我不想hash mode用于 SEO 相关的原因。
路由器模式参考:https : //router.vuejs.org/en/essentials/history-mode.html
我知道我可以这样做:
func randomFunc() {
// do stuff
go destroyObjectAfterXHours(4, "idofobject")
// do other stuff
}
func destroyObjectAfterXHours(hours int, id string) {
time.Sleep(hours * time.Hour)
destroyObject(id)
}
Run Code Online (Sandbox Code Playgroud)
但是如果我们想象destroyObjectAfterXHours在几分钟内被调用一百万次,这个解决方案就会很糟糕。
我希望有人可以分享一个更有效的解决方案来解决这个问题。
我一直在考虑一个潜在的解决方案,其中销毁时间和对象 id 存储在某处,然后会有一个 func 每 X 分钟遍历一次列表,销毁必须销毁的对象并删除它们的 id 和时间来自该信息存储位置的信息。这会是一个很好的解决方案吗?
我担心这也是一个糟糕的解决方案,因为您将不得不一直遍历包含数百万个项目的列表,然后必须有效地删除一些项目等。
我正在开发一个大项目,最终可能会包含数万行代码,对于当前的结构我喜欢这样:
main.go
controllers/NAME.go
models/NAME.go
Run Code Online (Sandbox Code Playgroud)
问题是控制器和模型目录包含大量文件,所有文件都使用package controllers和package models。因此我正在考虑像这样拆分它:
main.go
controllers/user/NAME.go
models/user/NAME.go
Run Code Online (Sandbox Code Playgroud)
控制器包中的用户文件可能包含routes.go、profile.go等文件。
controllersUser现在,我读到将包命名为or是不好的做法controllers_user,但我担心将两者命名可能是一个坏主意,因为package user它们是同一项目的一部分(即使它们位于不同的目录中)。
鉴于我的情况,您会推荐哪种命名结构?
我意识到位于控制器/用户中的文件将无法与模型/用户交互,即使它们共享相同的package user名称(当然除非我导入),而且我也知道我可以轻松导入 asuserControllers "controllers/user"和userModels "models/user",但我想知道这样做是否是一种不好的做法?
我已经创建了这个非常简单的测试程序。
package main
import (
"fmt"
"github.com/microcosm-cc/bluemonday"
"github.com/pressly/chi"
"github.com/russross/blackfriday"
"github.com/unrolled/render"
"net/http"
)
func main() {
r := chi.NewRouter()
r.Get("/", homepageGET)
http.ListenAndServe(":8080", r)
}
func homepageGET(w http.ResponseWriter, r *http.Request) {
Renderer := render.New(render.Options{
Directory: "frontend",
Extensions: []string{".tmpl", ".html"},
UnEscapeHTML: true,
})
unsafe := blackfriday.MarkdownCommon([]byte("**bolded text**"))
markdownContent := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
fmt.Print(string(markdownContent))
Renderer.HTML(w, http.StatusOK, "index", map[string]interface{}{
"content": fmt.Sprintf(string(markdownContent))})
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个 HTML 文件,除了:
<body>
{{ .content }}
</body>
Run Code Online (Sandbox Code Playgroud)
fmt.Print 命令打印“ <p><strong>bolded text</strong></p>”,而它被插入到 HTML 页面中为:“ <p><strong>bolded text</strong></p>”。
我相信它与转义的 HTML 相关,但对于展开/渲染包,我将其配置为未转义..我非常感谢任何帮助使测试程序正常工作(最好与展开/渲染一起)。
我想完全分离客户端和服务器,所以我创建了一个带有vue init webpack my-project. 在这个项目中,我将 vue-router 用于我的所有路由(这包括特殊路径,例如/user/SOMEID..
这是我的 routes.js 文件:
import App from './App.vue'
export const routes = [
{
path: '/',
component: App.components.home
},
{
path: '/user/:id',
component: App.components.userid
},
{
path: '*',
component: App.components.notFound
}
]
Run Code Online (Sandbox Code Playgroud)
当我使用npm run dev一切完美运行应用程序时。我现在准备部署到云中,所以我运行了npm run build. 由于我需要使用 HTTP 服务器,因此我决定也使用 Go 来实现……这是我的 Go 文件:
package main
import (
"fmt"
"github.com/go-chi/chi"
"github.com/me/myproject/server/handler"
"net/http"
"strings"
)
func main() {
r := chi.NewRouter()
distDir := "/home/me/code/go/src/github.com/me/myproject/client/dist/static"
FileServer(r, "/static", http.Dir(distDir)) …Run Code Online (Sandbox Code Playgroud)