diff --git a/hacking-go/learn02/casting/casting.go b/hacking-go/learn02/casting/casting.go new file mode 100644 index 0000000..fc13adb --- /dev/null +++ b/hacking-go/learn02/casting/casting.go @@ -0,0 +1,15 @@ +package main + +import ( + "fmt" +) + +func main() { + var a, b int = 20, 30 + // Need to convert a and b to float32 before the division + var div float32 = float32(a) / float32(b) + var divs float32 = float32(a/b) + // Cast float32 to int + var divInt = int(div) + fmt.Println(div, divInt, divs) +} diff --git a/hacking-go/learn02/variables/variables.go b/hacking-go/learn02/variables/variables.go index de34469..13341a6 100644 --- a/hacking-go/learn02/variables/variables.go +++ b/hacking-go/learn02/variables/variables.go @@ -16,6 +16,9 @@ func main() { var sampleString_single = "Hello tainguyen" fmt.Println("value variable is: ", a, b, d, f, sampleInt, sampleBoolean, sampleString) + fmt.Println(a, b, d, f, sampleInt, sampleBoolean, sampleString) + fmt.Println("value variable single is: ", sampleInt_single, sampleBoolean_single, sampleString_single) + fmt.Println(sampleInt_single, sampleBoolean_single, sampleString_single) } diff --git a/hacking-go/learn02/variables/variables02.go b/hacking-go/learn02/variables/variables02.go new file mode 100644 index 0000000..5bd6587 --- /dev/null +++ b/hacking-go/learn02/variables/variables02.go @@ -0,0 +1,17 @@ +package main + +import "fmt" + +func main() { + var ( + sampleInt = 500 + sampleBoolean = true + sampleString = "Hello" + ) + + sampleInt1, sampleBoolean1, sampleString1 := 4000, true, "Hello Tai Nguyen" + + fmt.Println(sampleInt, sampleBoolean, sampleString) + fmt.Println(sampleInt1, sampleBoolean1, sampleString1) + +} diff --git a/hacking-go/learn03/constants/constants.go b/hacking-go/learn03/constants/constants.go new file mode 100644 index 0000000..1979b91 --- /dev/null +++ b/hacking-go/learn03/constants/constants.go @@ -0,0 +1,20 @@ +package main + +import "fmt" + +const Whatever = "whatever" +const helloworld = "Hello World" + +const ( + Const1 = "Constant String" + Int1 = 12345 + True = true +) + +func main() { + fmt.Println(Whatever) + const One = 1 + fmt.Println(One) + fmt.Println(helloworld) + fmt.Println(Const1, Int1, True) +} diff --git a/hacking-go/learn03/rawstring/rawstring.go b/hacking-go/learn03/rawstring/rawstring.go new file mode 100644 index 0000000..d3d7052 --- /dev/null +++ b/hacking-go/learn03/rawstring/rawstring.go @@ -0,0 +1,38 @@ +package main + +import "fmt" + +func main() { + rawstr := + `First line +some new lines +more new lines +"double quotes" +` + fmt.Print(rawstr) + + rawstr1 := + `one line +two line +three line +"fours line" +` + + fmt.Print(rawstr1) + + rawstr2 := + `Line with tab here +Line with newline +and more lines +` + + fmt.Print(rawstr2) + + rawstr3 := + `This is a raw string with backticks: + It spans multiple lines and includes "quotes" and \backslashes\. +` + + fmt.Print(rawstr3) + +} diff --git a/hacking-go/learn04/for/forloop1.go b/hacking-go/learn04/for/forloop1.go new file mode 100644 index 0000000..cff3bfa --- /dev/null +++ b/hacking-go/learn04/for/forloop1.go @@ -0,0 +1,30 @@ +package main + +import "fmt" + +func main() { + + // var sum int + sum := 0 + for i := 0; i < 10; i++ { + sum += i + } + fmt.Println(sum) + + // // var sum int + sum1, i1 := 0, 0 + for i1 < 30 { + sum1 += i1 + i1++ + } + + fmt.Println(sum1) + + // // var sum int + sum2 := 1 + for i2 := 0; i2 < 40; i2++ { + sum2 += i2 + } + fmt.Println(sum2) + +} diff --git a/hacking-go/learn04/for/incdec.go b/hacking-go/learn04/for/incdec.go new file mode 100644 index 0000000..183f8a1 --- /dev/null +++ b/hacking-go/learn04/for/incdec.go @@ -0,0 +1,13 @@ +package main + +import "fmt" + +func main() { + + // var sum int + sum, i := 0, 0 + // This will not work + sum = i++ + + fmt.Println(sum) +} diff --git a/hacking-go/learn04/if/if.go b/hacking-go/learn04/if/if.go new file mode 100644 index 0000000..106a453 --- /dev/null +++ b/hacking-go/learn04/if/if.go @@ -0,0 +1,19 @@ +package main + +import "fmt" + +func main() { + a := 20 + b := 20 + + if b > a { + + fmt.Println(b, ">", a) + } + if b < a { + fmt.Println(b, "<", a) + } + if b == a { + fmt.Println(b, "=", a) + } +} diff --git a/hacking-go/learn04/if/if2.go b/hacking-go/learn04/if/if2.go new file mode 100644 index 0000000..5f76221 --- /dev/null +++ b/hacking-go/learn04/if/if2.go @@ -0,0 +1,11 @@ +package main + +import "fmt" + +func main() { + if var1 := 100; var1 > 20 { + fmt.Println("Value inside if:", var1) + } + // Cannot use the variable var1 here + +} diff --git a/hacking-go/learn04/if/ifelse.go b/hacking-go/learn04/if/ifelse.go new file mode 100644 index 0000000..a0fb799 --- /dev/null +++ b/hacking-go/learn04/if/ifelse.go @@ -0,0 +1,13 @@ +package main + +import "fmt" + +func main() { + if var1 := 10; var1 > 20 { + fmt.Println("Value inside if:", var1) + } else { + // Can use var1 here + fmt.Println("Value inside else:", var1) + } + +}