简体   繁体   中英

How to escape - (hyphen) using the groovy language

I am trying to declare a variable that requires hyphen as part of the design spec.

def user-svc = "accounts"

However, i am getting this error -

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)

Local variable names must be normal groovy identifiers , which doesn't include the hyphen.

Though any object property name can consist of arbitrary characters, when using quoted identifiers or subscript operator:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM