使用此代码获取ymd:
var ymd = '20190716';
var year = ymd.substring(0, 4);
var month = ymd.substring(4, 2);
var day = ymd.substring(6, 2);
console.log(year);
console.log(month);
console.log(day);Run Code Online (Sandbox Code Playgroud)
我认为从索引到长度获取子字符串的正确方法。所以...
想:
2019
07
16
Run Code Online (Sandbox Code Playgroud)
但是得到了:
2019
19
1907
Run Code Online (Sandbox Code Playgroud)
为什么?
我想用 gin 服务器提供一个 JSON 文件。并在 HTML 文件中设置一些自定义值。在其中使用 JavaScript 调用 JSON 文件。
我的应用程序结构:
.
??? main.go
??? templates
??? index.html
??? web.json
Run Code Online (Sandbox Code Playgroud)
我将这些基本源代码放入main.go文件中:
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
var router *gin.Engine
func main() {
router = gin.Default()
router.LoadHTMLGlob("templates/*")
router.GET("/web", func(c *gin.Context) {
c.HTML(
http.StatusOK,
"index.html",
gin.H{
"title": "Web",
"url": "./web.json",
},
)
})
router.Run()
}
Run Code Online (Sandbox Code Playgroud)
templates/index.html文件中的一些代码:
<!doctype html>
<html>
<head>
<title>{{ .title }}</title>
// ...
</head>
<body>
<div id="swagger-ui"></div>
// ...
<script>
window.onload = function() { …Run Code Online (Sandbox Code Playgroud) 首先,我发现了这个问题:
如何在 Golang 中设置 HTML <select> 元素的默认值?
但就我而言,我不知道该怎么做。
我有很多帖子数据并将它们呈现给模板。
控制器:
c.Data["posts"] = posts_data
Run Code Online (Sandbox Code Playgroud)
看法:
{{ range $post := .posts }}
<option value="{{ $post.ID }}">{{ $post.Name }}</option>
{{ end }}
Run Code Online (Sandbox Code Playgroud)
工作正常。
但如果改为:
控制器:
c.Data["posts"] = posts_data
c.Data["post_id"] = param_data
Run Code Online (Sandbox Code Playgroud)
看法:
{{ range $post := .posts }}
<option value="{{ $post.ID }}" {{ if eq $post.ID .post_id }}selected="selected"{{ end }}>{{ $post.Name }}</option>
{{ end }}
Run Code Online (Sandbox Code Playgroud)
出现错误:
template Execute err: template: posts/index.tpl:20:70: executing "posts/index.tpl" at <.post_id>: can't evaluate field post_id in type models.Post
Run Code Online (Sandbox Code Playgroud)
确实,这 …
D B:
column: name
value: NULL
Run Code Online (Sandbox Code Playgroud)
用gorm从DB中获取记录,然后
var name string
if name != nil {
//
}
Run Code Online (Sandbox Code Playgroud)
出现错误:
invalid operation: name != nil (mismatched types string and nil)
Run Code Online (Sandbox Code Playgroud)
是的,go中int类型使用的是nil,但是这种情况下DB中的值却不是NULL,""所以需要检查null,怎么办?