Skip to content

Commit a036e5c

Browse files
committed
golang notes
1 parent 4ffe4c3 commit a036e5c

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

content/posts/go-notes.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ author: "Kapil Agrawal"
66
comments: false
77
---
88

9-
# General rules
9+
## General rules
1010

1111
- Identifiers starting with an uppercase letter are exportable outside the package
1212

@@ -24,7 +24,7 @@ comments: false
2424

2525
---
2626

27-
# Slices
27+
## Slices
2828

2929
- slice internals
3030
- https://go.dev/blog/slices-intro
@@ -65,7 +65,7 @@ fmt.Println(cap(s)) // 3
6565

6666
---
6767

68-
# Maps
68+
## Maps
6969

7070
```go
7171
//declaration
@@ -114,7 +114,7 @@ func main(){
114114

115115
```
116116

117-
## The Comma-OK idiom
117+
### The Comma-OK idiom
118118

119119
- Unlike Python, Go does not have a built-in way to check if a key is present in a Map or not
120120
- 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(){
151151
}
152152
```
153153

154-
## Map operations
154+
### Map operations
155155

156156
```go
157157
//Define & initialize
@@ -181,7 +181,7 @@ for k,v := range totalWins{
181181

182182
---
183183

184-
# Structs
184+
## Structs
185185

186186
- Define a struct when you have related data that needs to be grouped together
187187

@@ -215,7 +215,7 @@ type struct employee{
215215

216216
```
217217

218-
## Anonymous struct
218+
### Anonymous struct
219219

220220
- Useful for marshaling a struct into JSON format and vice versa
221221
- 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{
244244

245245
---
246246

247-
# Functions
247+
## Functions
248248

249249
- Unlike Python, Go does not have named and optional input parameters
250250
- Although it is possible to _simulate_ python `**kwargs` using a struct
@@ -309,7 +309,7 @@ func main() {
309309

310310
```
311311

312-
## Function as a type
312+
### Function as a type
313313

314314
- Functions are first class citizens in Go. They can be used as types
315315

@@ -353,7 +353,7 @@ func main(){
353353
}
354354
```
355355

356-
## Anonymous function
356+
## Anonymous functions
357357

358358
- You can define new functions inside a function and assign them to variables. The inner functions don't have a name, hence anonymous.
359359

@@ -386,7 +386,7 @@ func AnonymousFunc(y int) {
386386

387387
```
388388

389-
# Closure
389+
## Closure
390390

391391
- Functions declared inside functions are special; they are closures.
392392
- Inner functions can access and modify the variables present in the outer function
@@ -475,7 +475,7 @@ func main() {
475475
}
476476
```
477477

478-
# Pointers
478+
## Pointers
479479

480480
- The zero value for a pointer is nil
481481
- 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()
585585

586586
```
587587

588-
## When to use a pointer receiver, when to use a value receiver
588+
## Pointer receiver vs. Value receiver
589589

590590
Use a pointer receiver when :
591591

@@ -600,13 +600,13 @@ Use a value receiver when :
600600
> - You do not intend to modify the receiver
601601
> - The receiver is a map, a func, a chan, a slice, a string, or an interface value (because internally it’s already a pointer)
602602
603-
# Interfaces
603+
## Interfaces
604604

605605
- An interface is like an abstract class in Python.
606606
- `interface` is a type which defines abstract behavior via methods.
607607
- An instance is said to have satisfied the contract (met a behavior) once it implements all the methods defined in the interface definition.
608608

609-
# Packages and Modules
609+
## Packages and Modules
610610

611611
### Downloading a Go module outside of a project
612612

0 commit comments

Comments
 (0)