43
43
- Write programs in a way that makes intentions clear
44
44
- Some particular approaches produce clearer codes
45
45
- Built-in types in Go and how to declare them
46
- - Go does things differently
47
- - Subtle differences with other languages
46
+ - Go does things differently: Subtle differences from other languages
48
47
- ** NOTE: All declared variables in Go must be used**
49
48
50
49
## Predeclared Types
58
57
59
58
## Zero Values
60
59
61
- - Declared variables with no initial values are assigned default Zero Values
60
+ - ** Declared variables with no initial values are assigned default * Zero Values***
62
61
- Explicit * Zero Value*
63
62
- Makes code clearer
64
63
- Removes sources of bugs
65
64
- [ More details on the docs page] ( https://go.dev/ref/spec#The_zero_value )
66
65
67
66
## Literals
68
67
69
- - Explicitly specified number, character, or string
68
+ - ** Explicitly specified number, character, or string**
70
69
- 4 common kinds of literals
71
70
- Integer literal
72
71
- Float Literal
@@ -123,7 +122,7 @@ func main() {
123
122
### Float Literal
124
123
125
124
- Sequence of numbers with decimal point to indicate the fractional portion
126
- - Can also have an exponent ` e ` or ` E ` with positive of negative number
125
+ - Can also have an exponent ` e ` or ` E ` with positive or negative number
127
126
- Can also be written in hexadecimal
128
127
- E.g. ` 0x12.34p5 `
129
128
- ` p ` indicates the exponent
@@ -225,6 +224,7 @@ func main() {
225
224
- ** In these cases, using * Raw String Literal* is more appropriate**
226
225
- Delimited with backticks <code >``</code >
227
226
- ** Can contain any character except backticks**
227
+ - If <code >\` </code > is required, try using <code >\` raw string\` + "`"</code > instead
228
228
- No escape character can be applied
229
229
- All characters are included as-is
230
230
@@ -242,15 +242,15 @@ func main() {
242
242
var greetings string = " Hello World!"
243
243
// Using Escapes
244
244
var greetingsLong string = " Greetings and \n\" Salutations\" !"
245
- var goPath string = " https :\\\\ go.dev "
245
+ var sysPath string = " C :\\\\ Windows \\ System32 "
246
246
// Using Raw String Literal
247
247
var greetingsRaw string = ` Greetings and
248
248
"Salutations"!`
249
249
var goPathRaw string = ` https://go.dev`
250
250
251
251
fmt.Println (" greetings =" , greetings)
252
252
fmt.Println (" greetingsLong =" , greetingsLong)
253
- fmt.Println (" goPath =" , goPath )
253
+ fmt.Println (" sysPath =" , sysPath )
254
254
fmt.Println (" greetingsRaw =" , greetingsRaw)
255
255
fmt.Println (" goPathRaw =" , goPathRaw)
256
256
}
@@ -501,7 +501,8 @@ func main() {
501
501
## Strings and Runes
502
502
503
503
- String is a built-in data type
504
- - Supports Unicode - We can put any unicode characters in strings
504
+ - Supports Unicode
505
+ - We can put any unicode characters in strings
505
506
- Zero-Value is ` "" `
506
507
- Default Type is ` string `
507
508
- ** Strings are immutable**
@@ -616,7 +617,7 @@ var x = 100
616
617
617
618
### Declaration Only With ` var `
618
619
619
- - We can declare only without assigning an initial value
620
+ - We can declare only, without assigning an initial value
620
621
- The type is required
621
622
- Assigns the * Zero-Value* of the type as the default value
622
623
@@ -666,7 +667,7 @@ var (
666
667
### Walrus Shortcut ` := `
667
668
668
669
- ` := ` can replace ` var ` declaration with type inference
669
- - It is preferred over ` var `
670
+ - ` := ` is preferred over ` var `
670
671
671
672
``` go
672
673
// Equivalent statements
@@ -704,10 +705,11 @@ name, age, isAdult := "Johnny", 26, true
704
705
- ` := ` is the most common inside functions
705
706
- Use declaration lists when declaring multiple variables
706
707
- Avoid ` := ` when:
708
+ - Declaring Package-level variables
707
709
- Initializing to zero-value
708
710
- Assigning an untyped constant
709
711
- Variable type cannot/should not be deduced from the assigned value
710
- - Prefer ` var x byte = 20 ` over ` x := byte(20) `
712
+ - Prefer ` var x byte = 20 ` over ` x := byte(20) `
711
713
- ** Sometimes, ` := ` creates a new variable than using an existing one (* Shadowing* )**
712
714
- In those cases, it is better to use ` var `
713
715
- ** NOTE: Only use the * Multiple declaration and assignment* style when assigning multiple values from a function return**
@@ -792,7 +794,7 @@ const total = x + y
792
794
- ** Untyped Constant** is the same as literal
793
795
- Has no type on its own
794
796
- Has default type when type cannot be inferred
795
- - ** Typed Constant** can be assigned directly only to a * "variable"* of the same type
797
+ - ** Typed Constant** can be assigned directly only a * "variable"* of the same type
796
798
- ** Usage depends on the intent**
797
799
- Constants to be used with multiple numeric types => * Keep untyped*
798
800
- Untyped Constants give more flexibility
@@ -818,7 +820,7 @@ var i64 int64 = tconst
818
820
819
821
## Unused Variables
820
822
821
- - ** Every * local * declared variables/constants must be used/read at least once**
823
+ - ** Every * locally * declared variables/constants must be used/read at least once**
822
824
- It is a compile-time error if a declared variable is not used
823
825
- As long as the variable is read once
824
826
- Go will not catch the unused ` x = 30 `
@@ -837,7 +839,7 @@ func main() {
837
839
```
838
840
839
841
- ** NOTE: This rule does not apply to * constants* and * package-level variables***
840
- - It is better to avoid package-level variables*
842
+ - It is better to avoid * package-level variables*
841
843
- Constants in Go are calculated at compile-time (cannot have side-effects)
842
844
- Can be eliminated if unused: They are excluded from the compiled binary
843
845
0 commit comments