微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

连接字符串中的变量

如何解决连接字符串中的变量

如何连接使用 Go 模板的字符串中的变量?这是原始行(来自 https://github.com/nginx-proxy/nginx-proxy/blob/master/nginx.tmpl):

{{ $host := trim $host }}
{{ $is_regexp := hasPrefix "~" $host }}
{{ $upstream_name := when $is_regexp (sha1 $host) $host }}

{{ $access_log := (or (and (not $.Env.disABLE_ACCESS_LOGS) "access_log /var/log/Nginx/access.log vhost;") "") }}

我正在尝试在该字符串中使用变量“host”,如下所示:

{{ $host := trim $host }}
{{ $is_regexp := hasPrefix "~" $host }}
{{ $upstream_name := when $is_regexp (sha1 $host) $host }}

{{ $access_log := (or (and (not $.Env.disABLE_ACCESS_LOGS) "access_log /var/log/Nginx/" + $host + "_access.log vhost;") "") }}

但我收到此错误

Unable to parse template: template: Nginx.tmpl:175: illegal number Syntax: "+"

解决方法

它使用 printf

{{ $host := trim $host }}
{{ $is_regexp := hasPrefix "~" $host }}
{{ $upstream_name := when $is_regexp (sha1 $host) $host }}
{{ $access_log := (or (and (not $.Env.DISABLE_ACCESS_LOGS) (printf "%s%s%s" "access_log /var/log/nginx/vhosts/" $host "_access.log vhost;")) "") }}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。