-
-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
groovy: Init helloworld example for groovy (#231)
- Loading branch information
Showing
4 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Install groovy | ||
- See https://groovy-lang.org/install.html (I prefer using SDK man) | ||
- Facing missing java issue while installing: Visit: [groovy-with-sdk-missing-java.md](.././../troubleshooting/installation/groovy-with-sdk-missing-java.md) | ||
|
||
# Basic groovy scripting | ||
- See [helloworld](./helloworld/) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Greetings! | ||
println "Welcome to groovy!" | ||
|
||
// Working with variables | ||
myName = "Tung" | ||
myAge = 28 | ||
println "${myName} is ${myAge} years old!" | ||
|
||
// Working with if else | ||
def number = 10 | ||
|
||
if (number > 5) { | ||
println("The number is greater than 5.") | ||
} else { | ||
println("The number is not greater than 5.") | ||
} | ||
|
||
// Working with switch case | ||
def myFruit = "Banana" | ||
|
||
switch (myFruit) { | ||
case "Apple": | ||
println("It's an apple.") | ||
break | ||
case "Banana": | ||
println("It's a banana.") | ||
break | ||
case "Cherry": | ||
println("It's a cherry.") | ||
break | ||
default: | ||
println("It's something else.") | ||
} | ||
|
||
// Working with for loop | ||
def fruits = ["Apple", "Banana", "Cherry", "Watermelon", "Elderberry"] | ||
|
||
for (fruit in fruits) { | ||
println("I like $fruit") | ||
} | ||
|
||
// Working with function | ||
def factorial(n) { | ||
if (n == 0) { | ||
return 1 | ||
} else { | ||
return n * factorial(n - 1) | ||
} | ||
} | ||
//-- Call the function and store the result in a variable | ||
def result = factorial(5) | ||
//-- Print the result | ||
println("Factorial of 5 is: $result") | ||
|
||
// Working with Map in groovy | ||
// Create a map with key-value pairs | ||
def person = [ | ||
"firstName": "John", | ||
"lastName": "Doe", | ||
"age": 30, | ||
"city": "New York" | ||
] | ||
// Access values in the map | ||
def firstName = person["firstName"] | ||
def age = person["age"] | ||
// Modify values in the map | ||
person["city"] = "Los Angeles" | ||
// Add a new key-value pair to the map | ||
person["email"] = "john.doe@example.com" | ||
// Iterate over the map | ||
person.each { key, value -> | ||
println("$key: $value") | ||
} | ||
|
||
// Working with try catch | ||
def divideNumbers(int dividend, int divisor) { | ||
try { | ||
def result = dividend / divisor | ||
return result | ||
} catch (ArithmeticException e) { | ||
println("An arithmetic exception occurred: ${e.message}") | ||
} finally { | ||
println("Finally block executed.") | ||
} | ||
} | ||
|
||
// Example usage: | ||
try { | ||
def devideResult = divideNumbers(10, 2) | ||
println("Result: $devideResult") | ||
} catch (Exception e) { | ||
println("An exception occurred: ${e.message}") | ||
} |
44 changes: 44 additions & 0 deletions
44
troubleshooting/installation/groovy-with-sdk-missing-java.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Groovy | ||
- Error `groovy: JAVA_HOME not set and cannot find javac to deduce location, please set JAVA_HOME.`: | ||
``` | ||
➜ ~ curl -s "https://get.sdkman.io" | bash | ||
➜ ~ source "$HOME/.sdkman/bin/sdkman-init.sh" | ||
➜ ~ source "$HOME/.sdkman/bin/sdkman-init.sh" | ||
➜ ~ sdk install groovy | ||
Downloading: groovy 4.0.14 | ||
In progress... | ||
##################################################################################################################################################################################### 100.0% | ||
Installing: groovy 4.0.14 | ||
Done installing! | ||
Setting groovy 4.0.14 as default. | ||
➜ ~ groovy | ||
groovy: JAVA_HOME not set and cannot find javac to deduce location, please set JAVA_HOME. | ||
➜ ~ groovy --version | ||
groovy: JAVA_HOME not set and cannot find javac to deduce location, please set JAVA_HOME. | ||
➜ ~ sdk install java | ||
Downloading: java 17.0.8.1-tem | ||
In progress... | ||
##################################################################################################################################################################################### 100.0% | ||
Repackaging Java 17.0.8.1-tem... | ||
Done repackaging... | ||
Installing: java 17.0.8.1-tem | ||
Done installing! | ||
Setting java 17.0.8.1-tem as default. | ||
➜ ~ groovy --version | ||
Groovy Version: 4.0.14 JVM: 17.0.8.1 Vendor: Eclipse Adoptium OS: Linux | ||
➜ ~ | ||
``` |