1+ /*
2+ * EXERCISE:
3+ * - Create examples using all types of operators in your language:
4+ * Arithmetic, logical, comparison, assignment, identity, membership, bitwise...
5+ * (Keep in mind that each language may have different ones)
6+ * - Using operations with operators of your choice, create examples
7+ * that represent all types of control structures that exist
8+ * in your language:
9+ * Conditionals, iterative, exceptions...
10+ * - You must print the result of all examples to the console.
11+ *
12+ * EXTRA DIFFICULTY (optional):
13+ * Create a program that prints to the console all numbers between
14+ * 10 and 55 (inclusive), even, and that are neither 16 nor multiples of 3.
15+ *
16+ * Surely by carefully reviewing the possibilities you have discovered something new.
17+ */
18+
19+ package main
20+
21+ import (
22+ "fmt"
23+ "os"
24+ )
25+
26+
27+ func main () {
28+ operators ()
29+ structuresControl ()
30+ exceptionsHandling ()
31+ panichHandling ()
32+ challenge ()
33+ }
34+
35+ func operators (){
36+ a := 10
37+ b := 5
38+
39+ // Arithmetic Operators:
40+ var addition = a + b
41+ var substraction = addition - 1 ;
42+ var multiplication = substraction * 10 ;
43+ var division = multiplication / 2 ;
44+ var module = division % 2 ;
45+ module ++
46+ module --
47+
48+ // Logical Operators:
49+ fmt .Println (a > 0 && b > 0 ) // Output: true
50+ fmt .Println (a > 0 || b < 0 ) // Output: true
51+
52+ // Assignment Operators:
53+ a += b
54+ fmt .Println (a ) // Output: 15
55+ a -= b
56+ fmt .Println (a ) // Output: 10
57+ a /= b
58+ fmt .Println (a ) // Output: 2
59+ a *= b
60+ fmt .Println (a ) // Output: 10
61+ a |= b
62+ fmt .Println (a ) // Output: 15
63+ a ^= b
64+ fmt .Println (a ) // Output: 10
65+
66+ // Identity operators
67+ c := a
68+ fmt .Println (a == c ) // Output: true
69+ d := & a
70+ e := & a
71+ fmt .Println (d == e )
72+ fmt .Println (& a ) // This is the memory address
73+
74+ // Membership operators
75+ fruits := []string {"apple" , "banana" , "orange" }
76+ fmt .Println ("apple" == fruits [0 ]) // Output: true
77+ fmt .Println ("grape" == fruits [0 ]) // Output: false
78+
79+ // Bitwise operators
80+ x := 10
81+ y := 5
82+ fmt .Println (x & y ) // Output: 2
83+ fmt .Println (x | y ) // Output: 15
84+ fmt .Println (x ^ y ) // Output: 13
85+ fmt .Println (^ x ) // Output: -11
86+ fmt .Println (x << 2 ) // Output: 40
87+ fmt .Println (x >> 2 ) // Output: 2
88+ }
89+
90+ func structuresControl (){
91+ x := 0 ;
92+
93+ // if-else
94+ if x > 0 {
95+ fmt .Println ("x is positive" )
96+ } else if x == 0 {
97+ fmt .Println ("x is zero" )
98+ } else {
99+ fmt .Println ("x is negative" )
100+ }
101+
102+ // switch
103+ day := "Monday"
104+ switch day {
105+ case "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" :
106+ fmt .Println ("Weekday" )
107+ case "Saturday" , "Sunday" :
108+ fmt .Println ("Weekend" )
109+ default :
110+ fmt .Println ("Invalid day" )
111+ }
112+
113+ // for loop
114+ for i := 0 ; i < 2 ; i ++ {
115+ fmt .Println (i )
116+ }
117+
118+ // Range loop
119+ fruits := []string {"apple" , "banana" , "orange" }
120+ for index , fruit := range fruits {
121+ fmt .Println (index , fruit )
122+ }
123+
124+ // While loop
125+ i := 0
126+ for i < 3 {
127+ fmt .Println (i )
128+ i ++
129+ }
130+ }
131+
132+ func divide (a , b int ) int {
133+ if b == 0 {
134+ panic ("Division by zero" )
135+ }
136+ return a / b
137+ }
138+
139+ func exceptionsHandling (){
140+ file , err := os .Open ("file.txt" )
141+ if err != nil {
142+ fmt .Println ("Error opening file:" , err )
143+ return
144+ }
145+ defer file .Close ()
146+ }
147+
148+ func panichHandling (){
149+ // this function in defer would recover from the error of ttrying dividing by 0
150+ defer func () {
151+ if err := recover (); err != nil {
152+ fmt .Println ("Recovered from panic:" , err )
153+ }
154+ }()
155+ result := divide (10 , 0 )
156+ fmt .Println (result )
157+ }
158+
159+ func challenge (){
160+ fmt .Println ("================ CHALLENGE ================" )
161+ // Create a program that prints to the console all numbers between
162+ // 10 and 55 (inclusive), even, and that are neither 16 nor multiples of 3.
163+
164+ i := 10
165+ for i < 56 {
166+ if (i % 2 == 0 && i != 16 && i % 3 != 0 ){
167+ fmt .Println (i )
168+ }
169+ i ++
170+ }
171+ }
0 commit comments