Skip to content

Commit

Permalink
groovy: Init helloworld example for groovy (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
tungbq authored Sep 9, 2023
2 parents 7ac156c + b2ac97b commit 33528be
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 1 deletion.
6 changes: 6 additions & 0 deletions topics/groovy/README.md
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/)
1 change: 0 additions & 1 deletion topics/groovy/basic/basic.groovy

This file was deleted.

93 changes: 93 additions & 0 deletions topics/groovy/helloworld/basic-concept.groovy
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 troubleshooting/installation/groovy-with-sdk-missing-java.md
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
➜ ~
```

0 comments on commit 33528be

Please sign in to comment.