1
1
mirror of https://github.com/namibia/awesome-cheatsheets.git synced 2024-11-22 04:45:16 +00:00

docs: start libs section of go cheatsheet

This commit is contained in:
LuanSilveiraSouza 2021-03-06 21:07:16 -03:00
parent 609f229542
commit 9349857693

View File

@ -18,6 +18,11 @@
- [Structs](#structs) - [Structs](#structs)
- [Maps](#maps) - [Maps](#maps)
- [Pointers](#pointers) - [Pointers](#pointers)
- [Methods and Interfaces](#methods-and-interfaces)
- [Errors](#errors)
- [Testing](#testing)
- Standard Libs
- [Package fmt](#package-fmt)
## Hello World ## Hello World
@ -27,7 +32,7 @@ package main
import "fmt" import "fmt"
func main() { func main() {
fmt.Println("Hello World!") fmt.Println("Hello Gophers!")
} }
``` ```
@ -90,6 +95,7 @@ $ go version
# Create Module # Create Module
$ go mod init [name] $ go mod init [name]
``` ```
Tip: By convention, modules names has the follow structure: Tip: By convention, modules names has the follow structure:
domain.com/user/module/package domain.com/user/module/package
@ -103,7 +109,7 @@ Example: github.com/spf13/cobra
## Basic Types ## Basic Types
| Type | Set of Values | Values | | Type | Set of Values | Values |
|:---------:|:-----------------:|:---------:| | :--------: | :----------------------------------------: | :-------------------------------------------: |
| bool | boolean | true/false | | bool | boolean | true/false |
| string | array of characters | needs to be inside "" | | string | array of characters | needs to be inside "" |
| int | integers | 32 or 64 bit integer | | int | integers | 32 or 64 bit integer |
@ -362,6 +368,7 @@ for _, value := range slice {
<hr/> <hr/>
## Functions ## Functions
```go ```go
// Functions acts as a scoped block of code // Functions acts as a scoped block of code
func sayHello() { func sayHello() {
@ -540,3 +547,115 @@ Obs: Unlike C, Go doesn't have pointer arithmetics.
[Return to Summary](#summary) [Return to Summary](#summary)
<hr/> <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/>