You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: content/posts/go-notes.md
+15-15
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ author: "Kapil Agrawal"
6
6
comments: false
7
7
---
8
8
9
-
# General rules
9
+
##General rules
10
10
11
11
- Identifiers starting with an uppercase letter are exportable outside the package
12
12
@@ -24,7 +24,7 @@ comments: false
24
24
25
25
---
26
26
27
-
# Slices
27
+
##Slices
28
28
29
29
- slice internals
30
30
-https://go.dev/blog/slices-intro
@@ -65,7 +65,7 @@ fmt.Println(cap(s)) // 3
65
65
66
66
---
67
67
68
-
# Maps
68
+
##Maps
69
69
70
70
```go
71
71
//declaration
@@ -114,7 +114,7 @@ func main(){
114
114
115
115
```
116
116
117
-
## The Comma-OK idiom
117
+
###The Comma-OK idiom
118
118
119
119
- Unlike Python, Go does not have a built-in way to check if a key is present in a Map or not
120
120
- Go’s map lookup does not throw errors for missing keys. Instead, it returns the zero value of the `ValueType` which makes this idiom essential for distinguishing between "key not present" and "key present with zero-value."
@@ -151,7 +151,7 @@ func main(){
151
151
}
152
152
```
153
153
154
-
## Map operations
154
+
###Map operations
155
155
156
156
```go
157
157
//Define & initialize
@@ -181,7 +181,7 @@ for k,v := range totalWins{
181
181
182
182
---
183
183
184
-
# Structs
184
+
##Structs
185
185
186
186
- Define a struct when you have related data that needs to be grouped together
187
187
@@ -215,7 +215,7 @@ type struct employee{
215
215
216
216
```
217
217
218
-
## Anonymous struct
218
+
###Anonymous struct
219
219
220
220
- Useful for marshaling a struct into JSON format and vice versa
221
221
- Remember, `struct` is a type. Anonymous struct types are NOT exportable outside the package where they’re declared because the type itself has no name, hence anonymous.
@@ -244,7 +244,7 @@ var Employee struct{
244
244
245
245
---
246
246
247
-
# Functions
247
+
##Functions
248
248
249
249
- Unlike Python, Go does not have named and optional input parameters
250
250
- Although it is possible to _simulate_ python `**kwargs` using a struct
@@ -309,7 +309,7 @@ func main() {
309
309
310
310
```
311
311
312
-
## Function as a type
312
+
###Function as a type
313
313
314
314
- Functions are first class citizens in Go. They can be used as types
315
315
@@ -353,7 +353,7 @@ func main(){
353
353
}
354
354
```
355
355
356
-
## Anonymous function
356
+
## Anonymous functions
357
357
358
358
- You can define new functions inside a function and assign them to variables. The inner functions don't have a name, hence anonymous.
359
359
@@ -386,7 +386,7 @@ func AnonymousFunc(y int) {
386
386
387
387
```
388
388
389
-
# Closure
389
+
##Closure
390
390
391
391
- Functions declared inside functions are special; they are closures.
392
392
- Inner functions can access and modify the variables present in the outer function
@@ -475,7 +475,7 @@ func main() {
475
475
}
476
476
```
477
477
478
-
# Pointers
478
+
##Pointers
479
479
480
480
- The zero value for a pointer is nil
481
481
- When passing a pointer variable to a function, you're actually passing _a copy_ of the pointer variable (memory address) which holds to the original data.
@@ -585,7 +585,7 @@ pet2 := &Animal // method set contains SuggestNames() and PetID()
585
585
586
586
```
587
587
588
-
## When to use a pointer receiver, when to use a value receiver
588
+
## Pointer receiver vs. Value receiver
589
589
590
590
Use a pointer receiver when :
591
591
@@ -600,13 +600,13 @@ Use a value receiver when :
600
600
> - You do not intend to modify the receiver
601
601
> - The receiver is a map, a func, a chan, a slice, a string, or an interface value (because internally it’s already a pointer)
602
602
603
-
# Interfaces
603
+
##Interfaces
604
604
605
605
- An interface is like an abstract class in Python.
606
606
-`interface` is a type which defines abstract behavior via methods.
607
607
- An instance is said to have satisfied the contract (met a behavior) once it implements all the methods defined in the interface definition.
0 commit comments