Skip to content

Commit

Permalink
Merge pull request #7 from tainguyenbp/feat/learning-hacking-with-gol…
Browse files Browse the repository at this point in the history
…ang03

learn hacking with golang
  • Loading branch information
tainguyenbp authored Jun 3, 2024
2 parents 80ad865 + 56c2bf1 commit fe04e1a
Show file tree
Hide file tree
Showing 14 changed files with 420 additions and 3 deletions.
9 changes: 6 additions & 3 deletions hacking-go/learn04/if/ifelse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package main
import "fmt"

func main() {
if var1 := 10; var1 > 20 {
if var1 := 5; var1 > 20 {
fmt.Println("Value inside if:", var1)
} else {
// Can use var1 here
fmt.Println("Value inside else:", var1)
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
}
}

}
17 changes: 17 additions & 0 deletions hacking-go/learn04/if/ifelse1.go
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
}
}

}
21 changes: 21 additions & 0 deletions hacking-go/learn04/if/ifelse2.go
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")
}
}
}
}
18 changes: 18 additions & 0 deletions hacking-go/learn04/if/ifelse3.go
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")
}
}
21 changes: 21 additions & 0 deletions hacking-go/learn04/switch/switch.go
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")
}
}
23 changes: 23 additions & 0 deletions hacking-go/learn04/switch/switch1.go
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
}
}
27 changes: 27 additions & 0 deletions hacking-go/learn04/switch/switch2.go
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.")
}
}
18 changes: 18 additions & 0 deletions hacking-go/learn04/switch/switch3.go
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")
}
}
49 changes: 49 additions & 0 deletions hacking-go/learn04/switch/switch4.go
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.")
}
}
71 changes: 71 additions & 0 deletions hacking-go/learn04/switch/switch5.go
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.")
}
}
63 changes: 63 additions & 0 deletions hacking-go/learn04/switch/switch6-advan.go
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.")
}
}
19 changes: 19 additions & 0 deletions hacking-go/learn04/switch/switch6.go
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")
}
}
Loading

0 comments on commit fe04e1a

Please sign in to comment.