mirror of
https://github.com/namibia/awesome-cheatsheets.git
synced 2024-11-21 20:35:12 +00:00
docs: start libs section of go cheatsheet
This commit is contained in:
parent
609f229542
commit
9349857693
@ -2,22 +2,27 @@
|
||||
|
||||
## Summary
|
||||
|
||||
- Introduction
|
||||
- [Hello World](#hello-world)
|
||||
- [Go CLI Commands](#go-cli-commands)
|
||||
- [Go Modules](#go-modules)
|
||||
- Basic
|
||||
- [Basic Types](#basic-types)
|
||||
- [Variables](#variables)
|
||||
- [Operators](#operators)
|
||||
- [Conditional Statements](#conditional-statements)
|
||||
- [Loops](#loops)
|
||||
- [Arrays](#arrays)
|
||||
- [Functions](#functions)
|
||||
- Advanced
|
||||
- [Structs](#structs)
|
||||
- [Maps](#maps)
|
||||
- [Pointers](#pointers)
|
||||
- Introduction
|
||||
- [Hello World](#hello-world)
|
||||
- [Go CLI Commands](#go-cli-commands)
|
||||
- [Go Modules](#go-modules)
|
||||
- Basic
|
||||
- [Basic Types](#basic-types)
|
||||
- [Variables](#variables)
|
||||
- [Operators](#operators)
|
||||
- [Conditional Statements](#conditional-statements)
|
||||
- [Loops](#loops)
|
||||
- [Arrays](#arrays)
|
||||
- [Functions](#functions)
|
||||
- Advanced
|
||||
- [Structs](#structs)
|
||||
- [Maps](#maps)
|
||||
- [Pointers](#pointers)
|
||||
- [Methods and Interfaces](#methods-and-interfaces)
|
||||
- [Errors](#errors)
|
||||
- [Testing](#testing)
|
||||
- Standard Libs
|
||||
- [Package fmt](#package-fmt)
|
||||
|
||||
## Hello World
|
||||
|
||||
@ -27,7 +32,7 @@ package main
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello World!")
|
||||
fmt.Println("Hello Gophers!")
|
||||
}
|
||||
```
|
||||
|
||||
@ -80,16 +85,17 @@ $ go version
|
||||
|
||||
## Go Modules
|
||||
|
||||
- Go projects are called **modules**
|
||||
- Each module has multiple **packages**
|
||||
- Each package should has a scoped functionality. Packages talk to each other to compose the code
|
||||
- A module needs at least one package, the **main**
|
||||
- The package main needs a entry function called **main**
|
||||
- Go projects are called **modules**
|
||||
- Each module has multiple **packages**
|
||||
- Each package should has a scoped functionality. Packages talk to each other to compose the code
|
||||
- A module needs at least one package, the **main**
|
||||
- The package main needs a entry function called **main**
|
||||
|
||||
```bash
|
||||
# Create Module
|
||||
$ go mod init [name]
|
||||
```
|
||||
|
||||
Tip: By convention, modules names has the follow structure:
|
||||
|
||||
domain.com/user/module/package
|
||||
@ -102,25 +108,25 @@ Example: github.com/spf13/cobra
|
||||
|
||||
## Basic Types
|
||||
|
||||
| Type | Set of Values | Values |
|
||||
|:---------:|:-----------------:|:---------:|
|
||||
| bool | boolean | true/false |
|
||||
| string | array of characters | needs to be inside "" |
|
||||
| int | integers | 32 or 64 bit integer |
|
||||
| int8 | 8-bit integers | [ -128, 128 ] |
|
||||
| int16 | 16-bit integers | [ -32768, 32767] |
|
||||
| int32 | 32-bit integers | [ -2147483648, 2147483647] |
|
||||
| int64 | 64-bit integers | [ -9223372036854775808, 9223372036854775807 ] |
|
||||
| uint8 | 8-bit unsigned integers | [ 0, 255 ] |
|
||||
| uint16 | 16-bit unsigned integers | [ 0, 65535 ] |
|
||||
| uint32 | 32-bit unsigned integers | [ 0, 4294967295 ] |
|
||||
| uint64 | 64-bit unsigned integers | [ 0, 18446744073709551615 ] |
|
||||
| float32 | 32-bit float | |
|
||||
| float64 | 64-bit float | |
|
||||
| complex64 | 32-bit float with real and imaginary parts | |
|
||||
| complex128 | 64-bit float with real and imaginary parts | |
|
||||
| byte | sets of bits | alias for uint8 |
|
||||
| rune | Unicode characters | alias for int32 |
|
||||
| Type | Set of Values | Values |
|
||||
| :--------: | :----------------------------------------: | :-------------------------------------------: |
|
||||
| bool | boolean | true/false |
|
||||
| string | array of characters | needs to be inside "" |
|
||||
| int | integers | 32 or 64 bit integer |
|
||||
| int8 | 8-bit integers | [ -128, 128 ] |
|
||||
| int16 | 16-bit integers | [ -32768, 32767] |
|
||||
| int32 | 32-bit integers | [ -2147483648, 2147483647] |
|
||||
| int64 | 64-bit integers | [ -9223372036854775808, 9223372036854775807 ] |
|
||||
| uint8 | 8-bit unsigned integers | [ 0, 255 ] |
|
||||
| uint16 | 16-bit unsigned integers | [ 0, 65535 ] |
|
||||
| uint32 | 32-bit unsigned integers | [ 0, 4294967295 ] |
|
||||
| uint64 | 64-bit unsigned integers | [ 0, 18446744073709551615 ] |
|
||||
| float32 | 32-bit float | |
|
||||
| float64 | 64-bit float | |
|
||||
| complex64 | 32-bit float with real and imaginary parts | |
|
||||
| complex128 | 64-bit float with real and imaginary parts | |
|
||||
| byte | sets of bits | alias for uint8 |
|
||||
| rune | Unicode characters | alias for int32 |
|
||||
|
||||
[Return to Summary](#summary)
|
||||
|
||||
@ -171,7 +177,7 @@ const pi = 3.1415
|
||||
[Return to Summary](#summary)
|
||||
|
||||
Arithmetic Operators
|
||||
| Symbol | Operation | Valid Types |
|
||||
| Symbol | Operation | Valid Types |
|
||||
|:---------:|:-------------:|:-------------:|
|
||||
| `+` | Sum | integers, floats, complex values, strings |
|
||||
| `-` | Difference | integers, floats, complex values |
|
||||
@ -186,7 +192,7 @@ Arithmetic Operators
|
||||
| `>>` | Right shift | integer >> unsigned integer |
|
||||
|
||||
Comparison Operators
|
||||
| Symbol | Operation |
|
||||
| Symbol | Operation |
|
||||
|:---------:|:-------------:|
|
||||
| `==` | Equal |
|
||||
| `!=` | Not equal |
|
||||
@ -196,7 +202,7 @@ Comparison Operators
|
||||
| `>=` | Greater or equal |
|
||||
|
||||
Logical Operators
|
||||
| Symbol | Operation |
|
||||
| Symbol | Operation |
|
||||
|:---------:|:-------------:|
|
||||
| `&&` | Conditional AND |
|
||||
| `||` | Conditional OR |
|
||||
@ -362,6 +368,7 @@ for _, value := range slice {
|
||||
<hr/>
|
||||
|
||||
## Functions
|
||||
|
||||
```go
|
||||
// Functions acts as a scoped block of code
|
||||
func sayHello() {
|
||||
@ -540,3 +547,115 @@ Obs: Unlike C, Go doesn't have pointer arithmetics.
|
||||
[Return to Summary](#summary)
|
||||
|
||||
<hr/>
|
||||
|
||||
## Methods and Interfaces
|
||||
|
||||
Go doesn't have classes. But you can implement methods, interfaces and almost everything contained in OOP, but in what gophers call "Go Way"
|
||||
|
||||
```go
|
||||
type Dog struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func (dog *Dog) bark() string {
|
||||
return dog.Name + " is barking!"
|
||||
}
|
||||
|
||||
dog := Dog{"Rex"}
|
||||
dog.bark() // Rex is barking!
|
||||
```
|
||||
|
||||
Interfaces are implicitly implemented. You don't need to inform that your struct are correctly implementing a interface if it already has all methods with the same name of the interface.
|
||||
All structs implement the `interface{}` interface. This empty interface means the same as `any`.
|
||||
|
||||
```go
|
||||
// Car implements Vehicle interface
|
||||
type Vehicle interface {
|
||||
Accelerate()
|
||||
}
|
||||
|
||||
type Car struct {
|
||||
|
||||
}
|
||||
|
||||
func (car *Car) Accelerate() {
|
||||
return "Car is moving on ground"
|
||||
}
|
||||
```
|
||||
|
||||
[Return to Summary](#summary)
|
||||
|
||||
<hr/>
|
||||
|
||||
## Errors
|
||||
|
||||
Go doesn't support `throw`, `try`, `catch` and other common error handling structures. Here, we use `error` package to build possible errors as a returning parameter in functions
|
||||
|
||||
```go
|
||||
import "errors"
|
||||
|
||||
// Function that contain a logic that can cause a possible exception flow
|
||||
func firstLetter(text string) (string, error) {
|
||||
if len(text) < 1 {
|
||||
return nil, errors.New("Parameter text is empty")
|
||||
}
|
||||
return string(text[0]), nil
|
||||
}
|
||||
|
||||
a, errorA := firstLetter("Wow")
|
||||
a // "W"
|
||||
errorA // nil
|
||||
|
||||
b, errorB := firstLetter("")
|
||||
b // nil
|
||||
errorB // Error("Parameter text is empty")
|
||||
```
|
||||
|
||||
[Return to Summary](#summary)
|
||||
|
||||
<hr/>
|
||||
|
||||
## Testing
|
||||
|
||||
Go has a built-in library to unit testing. In a separate file you insert tests for functionalities of a file and run `go test package` to run all tests of the actual package or `go test path` to run a specific test file.
|
||||
|
||||
```go
|
||||
// main.go
|
||||
func Sum(x, y int) int {
|
||||
return x + y
|
||||
}
|
||||
|
||||
// main_test.go
|
||||
import (
|
||||
"testing"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func TestSum(t *testing.T) {
|
||||
x, y := 2, 4
|
||||
expected := 2 + 4
|
||||
|
||||
if !reflect.DeepEqual(sum(x, y), expected) {
|
||||
t.Fatalf("Function Sum not working as expected")
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
[Return to Summary](#summary)
|
||||
|
||||
<hr/>
|
||||
|
||||
## Package `fmt`
|
||||
|
||||
```go
|
||||
import "fmt"
|
||||
|
||||
fmt.Print("Hello World") // Print in console
|
||||
fmt.Println("Hello World") // Print and add a new line in end
|
||||
fmt.Printf("%s is %d years old", "John", 32) // Print with formatting
|
||||
fmt.Errorf("User %d not found", 123) // Print a formatted error
|
||||
```
|
||||
|
||||
[Return to Summary](#summary)
|
||||
|
||||
<hr/>
|
Loading…
Reference in New Issue
Block a user