Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Go'返回数组中的唯一元素;s文本/模板包?_Go - Fatal编程技术网

如何使用Go'返回数组中的唯一元素;s文本/模板包?

如何使用Go'返回数组中的唯一元素;s文本/模板包?,go,Go,我是新手,我正在努力寻找一种方法,用Go模板语言从数组中返回唯一的变量。这是为了配置一些软件,我没有访问源代码来更改实际的程序,只有模板 我在围棋场上想出了一个例子: 现在它返回: The thing is: mice The thing is: mice The thing is: mice The thing is: mice The thing is: mice The thing is: mice The thing is: mice The thing is: toad The th

我是新手,我正在努力寻找一种方法,用Go模板语言从数组中返回唯一的变量。这是为了配置一些软件,我没有访问源代码来更改实际的程序,只有模板

我在围棋场上想出了一个例子:

现在它返回:

The thing is: mice
The thing is: mice
The thing is: mice
The thing is: mice
The thing is: mice
The thing is: mice
The thing is: mice
The thing is: toad
The thing is: toad
The thing is: mice
我想做的是制作一个模板,从输入数组中筛选重复项并仅返回:

The thing is: mice
The thing is: toad
我真的被卡住了,因为我知道在文档中几乎找不到任何数组操作方法。有人有什么建议吗

毒芹 对不起,我不太清楚,我在上班路上的公共汽车上写了这个问题

我没有权限访问模板之外的任何go代码。我有一个可以编辑的模板,在这个模板中我有一个数组,它可能有多个值,也可能没有多个值,我需要打印一次


我明白这不是模板的工作方式,但如果有一些肮脏的方式来做这件事,它将节省我几天的工作。

模板可以访问特定的自定义功能。这允许从模板中调用您自己的逻辑

文件中有一个综合性的例子,我在这里不再重复。关键一行是设置功能图并将其提供给模板:

 tmpl, err := template.New("titleTest").Funcs(funcMap).Parse(templateText)
然后可以在模板中访问

 {{myCustomFuction .}}
既然现在已经确定需要更多的go代码以映射函数的形式来实现,我想我会分享一个想法,可以完成这项工作。如果您可以控制“go”程序无法修改运行的模板,则可以进行多次传递。我还假设您使用的是linux。大概是这样的:

goexe 'first template'    // this writes to 'text file'
cat textfile | sort | uniq > 'text file'
goexe 'second template'   // your desired output

您可以通过
template.FuncMap
为模板创建自己的函数:

arr := []string{
    "mice",
    "mice",
    "mice",
    "mice",
    "mice",
    "mice",
    "mice",
    "toad",
    "toad",
    "mice",
}

customFunctions := template.FuncMap{"unique" : unique}

tmpl, err := template.New("test").Funcs(customFunctions).Parse("{{range $index, $thing := unique $}}The thing is: {{$thing}}\n{{end}}")
其中,
unique
定义为:

func unique(e []string) []string {
    r := []string{}

    for _, s := range e {
        if !contains(r[:], s) {
            r = append(r, s)
        }
    }
    return r
}

func contains(e []string, c string) bool {
    for _, s := range e {
        if s == c {
            return true
        }
    }
    return false
}
输出:

The thing is: mice
The thing is: toad
(最好使用
地图
。但这会给出基本的想法)

也就是说,您是否考虑过在模板之外过滤此内容?那对你来说会更好。。然后,您可以在模板中的实际切片上进行迭代

 {{myCustomFuction .}}

工作示例:

PS。我不知道数组中的哪个元素是我所在的元素,我只需要输出唯一的数组元素。我不确定我是否理解这将如何保证唯一性?我理解错误。您需要研究的是编写您自己的函数,您可以从模板中调用该函数。但本质上它必须符合正确的
接口
。请注意,编写的
unique
函数只允许len 10的字符串数组。一旦@rickard有了一个有效的解决方案,当样本数据发生变化时,它将不允许出现9或11,这可能会导致问题。True。固定的。对不起!(咖啡来了!)谢谢你的回答!我无法访问模板之外的代码。模板将被加载并配置到软件。在模板中是否没有方法执行此操作?它不一定很漂亮:)我不知道不。我不知道在模板中维护状态的任何方法-你需要这样才能知道什么是唯一的,什么不是。@AmirA.Shabani这是一个6岁的答案,我不能100%确定为什么我选择使用这种语法。有时我会尝试在我的答案中解释其他可能性,这种语法允许OP使用固定大小的数组作为输入。然后使用
r[:]
将从数组中创建一个切片,并按预期继续工作。除此之外,我不完全确定我为什么选择它。