【问题标题】:How to escape - (hyphen) using the groovy language如何逃脱 - (连字符)使用 groovy 语言
【发布时间】:2020-11-17 16:29:08
【问题描述】:

我正在尝试声明一个需要连字符作为设计规范一部分的变量。

def user-svc = "accounts"

但是,我收到了这个错误 -

https://www.tutorialspoint.com/execute_groovy_online.php

$groovy main.groovy
Hello world
Caught: groovy.lang.MissingPropertyException: No such property: user for class: main
groovy.lang.MissingPropertyException: No such property: user for class: main
    at main.run(main.groovy:3)

【问题讨论】:

  • 想知道为什么设计规范指定的变量名无效? ...

标签: jenkins groovy jenkins-pipeline jenkins-plugins groovyshell


【解决方案1】:

局部变量名必须是normal groovy identifiers,不包括连字符。​​

虽然任何对象的属性名称都可以由任意字符组成,但在使用quoted identifiers 或下标运算符时:

def m = [:]
m.'user-svc' = "accounts"
println m.'user-svc'
println m['user-svc']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多