https://github.com/weblancaster/go-studies
Go lang studies
https://github.com/weblancaster/go-studies
Last synced: 10 months ago
JSON representation
Go lang studies
- Host: GitHub
- URL: https://github.com/weblancaster/go-studies
- Owner: weblancaster
- License: mit
- Created: 2020-04-10T08:23:25.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-04-17T06:05:55.000Z (about 6 years ago)
- Last Synced: 2024-04-15T03:17:05.004Z (about 2 years ago)
- Language: Go
- Size: 10.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## package main
`package main` declaration is the first thing in a go file and its meant to tell go what is the workspace/project/package the file is part of. \
The reason we have called `main` is because go has two types of packages, `executable` and `resusable`.
**Executable:** \
Is when the code that is compiled and bundled as executable file. Must have a `func` called `main`.
**Reusable:** \
Are packages that are used as dependencies and not generated as executable. One way to differ a resusable from executable is the by the `package main` declaration.
## import
import is used to get code/access defined in another package
## file pattern
We will always find this file pattern in go files.
```
// package declaration
package main
// import required/needed packages
import ("fmt")
// functions and whatnots to do things
func main() {}
```
## pointers
Let's say we have an instance of a struct called `person` which we have the field `name` with the value as `"jon doe"` and we try to change this value like so:
```go
p := person{
name: "jon doe"
}
p.name = "new name"
```
The above code won't work because at the time of the instantiation we are saving/allocating in memory the person object with name and value, and then when trying to change the name although go won't through any errors we won't be changing the values/data in the original allocation of the person object.
For that we need to use pointer operators:
`&` Get the memory address where originally saved, example `pPointer := &p` \
`*` Get the value of the memory address is pointing to, example `*pPointer`
That being said, Go will allow to omit the `&` when the receiver is a pointer "type".
When to think about the pointers being passed arround? when using value types such as `int`, `float`, `string`, `bool` and `structs`
## channels
Channels are a way to communicate between go routines, first we need to `make` a channel and pass the type of the messages (which can also be channels) `c := make(chan string)` then we need to pass the channel to the function that uses the go routine `go doSomethingFoo(c)` and finally use the channels by sending `c <- "some message"` and receiving `<- c`