-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from tainguyenbp/feat/learning-hacking-with-gol…
…ang03 learn hacking with golang
- Loading branch information
Showing
14 changed files
with
420 additions
and
3 deletions.
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
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,17 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
var1 := 3 | ||
if var1 > 20 { | ||
fmt.Println("Value inside if:", var1) | ||
} else { | ||
if var1 > 5 { | ||
fmt.Println("Value inside nested if:", var1) // This block executes if var1 <= 20 and var1 > 5 | ||
} else { | ||
fmt.Println("Value inside nested else:", var1) // This block executes if var1 <= 5 | ||
} | ||
} | ||
|
||
} |
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,21 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
var var1 int = 10 // Declare and initialize var1 | ||
|
||
if var1 > 20 { | ||
fmt.Println("var1 is greater than 20") | ||
} else { | ||
if var1 > 15 { | ||
fmt.Println("var1 is greater than 15 but less than or equal to 20") | ||
} else { | ||
if var1 > 10 { | ||
fmt.Println("var1 is greater than 10 but less than or equal to 15") | ||
} else { | ||
fmt.Println("var1 is 10 or less") | ||
} | ||
} | ||
} | ||
} |
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,18 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
var var1 int = 10 | ||
var var2 int = 25 | ||
|
||
if var1 > 20 && var2 > 30 { | ||
fmt.Println("var1 is greater than 20 and var2 is greater than 30") | ||
} else if var1 > 15 || var2 > 20 { | ||
fmt.Println("Either var1 is greater than 15 or var2 is greater than 20") | ||
} else if var1 == 10 { | ||
fmt.Println("var1 is exactly 10") | ||
} else { | ||
fmt.Println("None of the conditions were met") | ||
} | ||
} |
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,21 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" // This is not cryptographically secure! | ||
"time" | ||
) | ||
|
||
func main() { | ||
// Seeding rand | ||
rand.Seed(time.Now().UnixNano()) | ||
fmt.Println("Choosing a random number:") | ||
switch num := rand.Intn(3); num { | ||
case 1: | ||
fmt.Println("1") | ||
case 2: | ||
fmt.Println("2") | ||
default: | ||
fmt.Println("3") | ||
} | ||
} |
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,23 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
func main() { | ||
// Seeding rand with the current time in nanoseconds to get different results each time | ||
rand.Seed(time.Now().UnixNano()) | ||
fmt.Println("Choosing a random number:") | ||
|
||
// Generating a random number between 0 and 2 (inclusive) | ||
switch num := rand.Intn(3); num { | ||
case 1: | ||
fmt.Println("1") | ||
case 2: | ||
fmt.Println("2") | ||
default: | ||
fmt.Println("3") // This will handle the case when num is 0 | ||
} | ||
} |
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,27 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
var num int | ||
fmt.Println("Please enter a number:") | ||
|
||
// Reading input from the user | ||
_, err := fmt.Scanln(&num) | ||
if err != nil { | ||
fmt.Println("Please enter number type int, error reading input:", err) | ||
return | ||
} | ||
|
||
fmt.Println("You entered:", num) | ||
|
||
// Using switch statement to handle different cases | ||
switch num { | ||
case 1: | ||
fmt.Println("You entered one.") | ||
case 2: | ||
fmt.Println("You entered two.") | ||
default: | ||
fmt.Println("You entered a number other than one or two.") | ||
} | ||
} |
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,18 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
var var1 int = 10 // Declare and initialize var1 | ||
|
||
switch { | ||
case var1 > 20: | ||
fmt.Println("var1 is greater than 20") | ||
case var1 > 15: | ||
fmt.Println("var1 is greater than 15 but less than or equal to 20") | ||
case var1 > 10: | ||
fmt.Println("var1 is greater than 10 but less than or equal to 15") | ||
default: | ||
fmt.Println("var1 is 10 or less") | ||
} | ||
} |
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,49 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
var num int | ||
fmt.Println("Please enter a number:") | ||
|
||
// Reading input from the user | ||
_, err := fmt.Scanln(&num) | ||
if err != nil { | ||
fmt.Println("Please enter number type int, error reading input:", err) | ||
return | ||
} | ||
|
||
fmt.Println("You entered:", num) | ||
|
||
// Using a switch statement to handle different cases | ||
switch { | ||
case num < 0: | ||
fmt.Println("You entered a negative number.") | ||
case num == 0: | ||
fmt.Println("You entered zero.") | ||
case num > 0 && num <= 10: | ||
fmt.Println("You entered a positive number between 1 and 10.") | ||
case num > 10 && num <= 20: | ||
fmt.Println("You entered a positive number between 11 and 20.") | ||
fallthrough | ||
case num > 20 && num <= 30: | ||
fmt.Println("You entered a positive number between 21 and 30.") | ||
fallthrough | ||
case num > 30: | ||
fmt.Println("You entered a number greater than 30.") | ||
default: | ||
fmt.Println("This case should never be reached.") | ||
} | ||
|
||
// Example of handling multiple specific cases | ||
switch num { | ||
case 1, 2, 3: | ||
fmt.Println("You entered a small number (1, 2, or 3).") | ||
case 10, 20, 30: | ||
fmt.Println("You entered a round number (10, 20, or 30).") | ||
default: | ||
fmt.Println("You entered a number that is neither small nor round.") | ||
} | ||
} |
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,71 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
var num int | ||
fmt.Println("Please enter a number:") | ||
|
||
// Reading input from the user | ||
_, err := fmt.Scanln(&num) | ||
if err != nil { | ||
fmt.Println("Error reading input:", err) | ||
return | ||
} | ||
|
||
fmt.Println("You entered:", num) | ||
|
||
// Using a switch statement to handle different cases | ||
switch { | ||
case num < 0: | ||
fmt.Println("You entered a negative number.") | ||
case num == 0: | ||
fmt.Println("You entered zero.") | ||
case num > 0 && num <= 10: | ||
if num == 5 { | ||
fmt.Println("You entered exactly five.") | ||
} else { | ||
fmt.Println("You entered a positive number between 1 and 10.") | ||
} | ||
case num > 10 && num <= 20: | ||
if num%2 == 0 { | ||
fmt.Println("You entered an even number between 11 and 20.") | ||
} else { | ||
fmt.Println("You entered an odd number between 11 and 20.") | ||
} | ||
fallthrough | ||
case num > 20 && num <= 30: | ||
if num%5 == 0 { | ||
fmt.Println("You entered a multiple of 5 between 21 and 30.") | ||
} else { | ||
fmt.Println("You entered a number between 21 and 30.") | ||
} | ||
fallthrough | ||
case num > 30: | ||
if num > 50 { | ||
fmt.Println("You entered a number greater than 50.") | ||
} else { | ||
fmt.Println("You entered a number between 31 and 50.") | ||
} | ||
default: | ||
fmt.Println("This case should never be reached.") | ||
} | ||
|
||
// Example of handling multiple specific cases with nested if-else | ||
switch num { | ||
case 1, 2, 3: | ||
fmt.Println("You entered a small number (1, 2, or 3).") | ||
if num == 2 { | ||
fmt.Println("Two is the only even small number.") | ||
} | ||
case 10, 20, 30: | ||
fmt.Println("You entered a round number (10, 20, or 30).") | ||
if num == 20 { | ||
fmt.Println("Twenty is a nice round number.") | ||
} | ||
default: | ||
fmt.Println("You entered a number that is neither small nor round.") | ||
} | ||
} |
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,63 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" // This is not cryptographically secure! | ||
"time" | ||
) | ||
|
||
func main() { | ||
// Seeding rand | ||
rand.Seed(time.Now().UnixNano()) | ||
fmt.Println("Choosing a random number:") | ||
num := rand.Intn(100) // Generate a random number between 0 and 99 | ||
fmt.Printf("Random number is: %d\n", num) | ||
|
||
// Using switch with advanced conditions and nested if-else | ||
switch { | ||
case num < 20: | ||
fmt.Println("Less than 20") | ||
if num < 10 { | ||
fmt.Println("Less than 10") | ||
} else { | ||
fmt.Println("Between 10 and 19") | ||
} | ||
case num >= 20 && num < 50: | ||
fmt.Println("Between 20 and 49") | ||
if num%2 == 0 { | ||
fmt.Println("Even number") | ||
} else { | ||
fmt.Println("Odd number") | ||
} | ||
case num >= 50 && num < 80: | ||
fmt.Println("Between 50 and 79") | ||
if num%5 == 0 { | ||
fmt.Println("Multiple of 5") | ||
} else { | ||
fmt.Println("Not a multiple of 5") | ||
} | ||
default: | ||
fmt.Println("80 or more") | ||
if num > 90 { | ||
fmt.Println("Greater than 90") | ||
} else { | ||
fmt.Println("Between 80 and 89") | ||
} | ||
} | ||
|
||
// Example of handling multiple specific cases with nested if-else | ||
switch num { | ||
case 1, 2, 3: | ||
fmt.Println("You entered a small number (1, 2, or 3).") | ||
if num == 2 { | ||
fmt.Println("Two is the only even small number.") | ||
} | ||
case 10, 20, 30: | ||
fmt.Println("You entered a round number (10, 20, or 30).") | ||
if num == 20 { | ||
fmt.Println("Twenty is a nice round number.") | ||
} | ||
default: | ||
fmt.Println("You entered a number that is neither small nor round.") | ||
} | ||
} |
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,19 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" // This is not cryptographically secure! | ||
"time" | ||
) | ||
|
||
func main() { | ||
// Seeding rand | ||
rand.Seed(time.Now().UnixNano()) | ||
fmt.Println("Choosing a random number:") | ||
switch num := rand.Intn(100); { | ||
case num < 50: | ||
fmt.Println("Less than 50") | ||
default: | ||
fmt.Println("More than 50") | ||
} | ||
} |
Oops, something went wrong.