Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dongwq/5dfcda3a88f629ee4a96b88a829b9462 to your computer and use it in GitHub Desktop.

Select an option

Save dongwq/5dfcda3a88f629ee4a96b88a829b9462 to your computer and use it in GitHub Desktop.
Using value of a variable as map key in Groovy

If you want to use a variable value as key of your map, then you should wrap your key with parenthesis. It makes key to be considered as expression instead of String literal.

String keyVar = "mykey"

def map0 = [keyVar : 12]
assert map0.keyVar == 12
assert !map0.mykey

def map1 = [(keyVar) : 12]
assert map1.mykey == 12
assert !map1.keyVar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment