【问题标题】:Golang - Text/template return key valueGolang - 文本/模板返回键值
【发布时间】:2018-08-31 17:10:27
【问题描述】:

我需要从文本模板键值中返回,就像下面的注释和命令

 #Description for npm install 
   npm install
 #Description for test
   npm test
 #Description for test2 
   run test2

为此,我创建了如下函数:

// example with switch
func (d Dependency) TypeCommand() Command {
    switch d.Type {
    case "runner":

        cmd1 := Command{"#Description for npm install", "npm install"}
        cmd2 := Command{"#Description for test", "npm test"}
        cmd3 := Command{"#Description for test2", "run test2"}

    case "runner2":
        return "test 2"

    }
    return "command_baz"
}

模板是:

const tmpl = `
{{- range .File.Dependency}}

{{.TypeCommand}}
{{end}}`

type Command struct {
    Info    string
    Command string
}

当我将模板更改为以下内容时,出现错误:

const tmpl = `
    {{- range .File.Dependency}}

     {{  TypeCommand .}}
   {{ range .Command}}
    {{ .Info }}
    {{ .Command }}
   {{end}}
  {{end}}
        '

executing "tmpl3.txt" at <.Command>: can't evaluate field Command in type *Dependency

我使用this 作为参考。

【问题讨论】:

  • 如果每次调用都需要--type 选项,您可以考虑改用子命令:extract format --format=jsonextract order --format=xml
  • 是的,我就是这个意思
  • TypeCommand 为什么要返回 String 并提到返回类型为 Dependency ?
  • @TarunLalwani - 不,这是错误的,我需要返回键值才能从模板中打印出来,知道怎么做吗?
  • 如果你的Command struct 和Command struct 字段被命名为不同的东西会很有帮助。

标签: go


【解决方案1】:

您收到的错误消息是因为您只是丢弃了TypeCommand 的返回值,而不是将其传递给您尝试访问其结构字段的位置。我们可以解决这个问题,但这可能不是您想要做的,因为您的 TypeCommand 函数看起来应该返回多个命令而不是单个命令。所以让我们先重写一下:

func (d Dependency) TypeCommand() []Command {
    switch d.Type {
    case "runner":

        return []Command{
          Command{"#Description for npm install", "npm install"},
          Command{"#Description for test", "npm test"},
          Command{"#Description for test2", "run test2"},
        }

    case "runner2":
        return []Command{Command{"#test 2", "foo"}}

    }
    return []Command{Command{"#command_baz", "baz"}}
}

现在我们返回了多个命令,我们可以在模板中对它们进行范围覆盖,它们将被自动绑定。我将您的模板稍微调整为以下内容:

const tmpl = `
{{- range .File.Dependency}}
  {{- range .TypeCommand }}
{{ .Info}}
  {{ .Command}}
{{- end}}{{end}}`

当我在 Go Playground 中运行它时,我得到了以下输出,这似乎是你想要的:

#Description for npm install
  npm install
#Description for test
  npm test
#Description for test2
  run test2
#test 2
  foo

【讨论】:

  • 谢谢,我现在试试,让你知道
  • 我现在试了,我也遇到了同样的错误,你能让操场工作吗?
  • 请看:play.golang.org/p/o16HBPv7agr,我必须按原样使用TypeCommand的功能,知道如何克服它吗?
  • func 结构应该像 func TypeCommand(d Dependency) []Command { 而不是像 func (d Dependency) TypeCommand() []Command { ,我怎样才能让它像这样工作?
  • 如果您坚持使用您在更新示例中提供的 func 结构,则需要使用 FuncMap,根据此操场示例:play.golang.org/p/L5lqvWTyzlV
【解决方案2】:
    package main

import (
    "os"
    "text/template"
)

type File struct {
    TypeVersion string `yaml:"_type-version"`
    Dependency  []Dependency
}

type Dependency struct {
    Name string
    Type string
    CWD  string
}

type Command struct {
    Info    string
    Command string
}

func  (d Dependency) TypeCommand() []Command {
    switch d.Type {
    case "runner":

        return []Command{
          {"#Description for npm install", "npm install"},
          {"#Description for test", "npm test"},
          {"#Description for test2", "run test2"},
        }

    case "runner2":
        return []Command{{"#test 2", "foo"}}

    }
    return []Command{{"#command_baz", "baz"}}
}

type Install map[string]string

const tmpl = `
{{- range .File.Dependency}}
  {{- range .TypeCommand }}
{{ .Info}}
  {{ .Command}}
{{- end}}{{end}}`

type API map[string]string

func main() {
    f := new(File)
    f.Dependency = []Dependency{{
        Name: "ui",
        Type: "runner",
        CWD:  "/ui",
    }, {
        Name: "ui2",
        Type: "runner2",
        CWD:  "/ui2",
    }}

    t, err := template.New("t").Parse(tmpl)
    if err != nil {
        panic(err)
    }

    var data struct {
        File *File
        API  API
    }
    data.File = f

    if err := t.Execute(os.Stdout, data); err != nil {
        panic(err)
    }
}

这应该可以正常工作。

主要问题是依赖方法的返回类型

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    相关资源
    最近更新 更多