小编020*_*402的帖子

如何在 Swagger UI 中隐藏模型部分?

我使用Swagger UI来显示 API 文档。默认情况下,它会在底部显示“模型”部分:

在此处输入图片说明

如何隐藏它?

swagger-ui

15
推荐指数
3
解决办法
2万
查看次数

如何使用JavaScript通过子字符串获取ymd

使用此代码获取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)

为什么?

javascript

4
推荐指数
1
解决办法
57
查看次数

如何在 Gin 路由器中渲染静态文件?

我想用 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)

static json go server go-gin

4
推荐指数
1
解决办法
1万
查看次数

如何设置默认选择选项以使用 Golang 选择标签?

首先,我发现了这个问题:

如何在 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)

确实,这 …

go

3
推荐指数
1
解决办法
2576
查看次数

如何用go检查空字符串类型?

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,怎么办?

go

-3
推荐指数
1
解决办法
2万
查看次数

标签 统计

go ×3

go-gin ×1

javascript ×1

json ×1

server ×1

static ×1

swagger-ui ×1