Golang - 文本/模板返回键值

时间:2018-03-22 10:03:47

标签: go

我需要返回文本模板键值,这将像注释和命令,如下面的

 #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作为参考。

2 个答案:

答案 0 :(得分:4)

您收到的错误消息是因为您只是丢弃了TypeCommand的返回值,而不是将其传递到您尝试访问其struct字段的位置。我们可以解决这个问题,但这可能不是你想要做的事情,因为你的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

答案 1 :(得分:-1)

    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)
    }
}

这应该正常工作。

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