docs: add syntax section to go cheatsheet
This commit is contained in:
parent
d3a4a69ce9
commit
3775de44b5
|
@ -9,6 +9,10 @@
|
||||||
- Syntax
|
- Syntax
|
||||||
- [Basic Types](#basic-types)
|
- [Basic Types](#basic-types)
|
||||||
- [Variables](#variables)
|
- [Variables](#variables)
|
||||||
|
- [Operators](#operators)
|
||||||
|
- [Conditional Statements](#conditional-statements)
|
||||||
|
- [Loops](#loops)
|
||||||
|
- [Functions](#functions)
|
||||||
|
|
||||||
## Hello World
|
## Hello World
|
||||||
|
|
||||||
|
@ -22,6 +26,10 @@ func main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
[Return to Summary](#summary)
|
||||||
|
|
||||||
|
<hr/>
|
||||||
|
|
||||||
## Go CLI Commands
|
## Go CLI Commands
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
@ -61,6 +69,10 @@ $ go env
|
||||||
$ go version
|
$ go version
|
||||||
```
|
```
|
||||||
|
|
||||||
|
[Return to Summary](#summary)
|
||||||
|
|
||||||
|
<hr/>
|
||||||
|
|
||||||
## Go Modules
|
## Go Modules
|
||||||
|
|
||||||
- Go projects are called **modules**
|
- Go projects are called **modules**
|
||||||
|
@ -79,6 +91,10 @@ domain.com/user/module/package
|
||||||
|
|
||||||
Example: github.com/spf13/cobra
|
Example: github.com/spf13/cobra
|
||||||
|
|
||||||
|
<hr/>
|
||||||
|
|
||||||
|
[Return to Summary](#summary)
|
||||||
|
|
||||||
## Basic Types
|
## Basic Types
|
||||||
|
|
||||||
| Type | Set of Values | Values |
|
| Type | Set of Values | Values |
|
||||||
|
@ -101,6 +117,10 @@ Example: github.com/spf13/cobra
|
||||||
| byte | sets of bits | alias for uint8 |
|
| byte | sets of bits | alias for uint8 |
|
||||||
| rune | Unicode characters | alias for int32 |
|
| rune | Unicode characters | alias for int32 |
|
||||||
|
|
||||||
|
[Return to Summary](#summary)
|
||||||
|
|
||||||
|
<hr/>
|
||||||
|
|
||||||
## Variables
|
## Variables
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
@ -133,4 +153,186 @@ var boolean bool // ""
|
||||||
|
|
||||||
i := 1.234 // float
|
i := 1.234 // float
|
||||||
int(i) // 1
|
int(i) // 1
|
||||||
```
|
|
||||||
|
// Constants
|
||||||
|
const pi = 3.1415
|
||||||
|
```
|
||||||
|
|
||||||
|
<hr/>
|
||||||
|
|
||||||
|
## Operators
|
||||||
|
|
||||||
|
[Return to Summary](#summary)
|
||||||
|
|
||||||
|
Arithmetic Operators
|
||||||
|
| Symbol | Operation | Valid Types |
|
||||||
|
|:---------:|:-------------:|:-------------:|
|
||||||
|
| `+` | Sum | integers, floats, complex values, strings |
|
||||||
|
| `-` | Difference | integers, floats, complex values |
|
||||||
|
| `*` | Product | integers, floats, complex values |
|
||||||
|
| `/` | Quotient | integers, floats, complex values |
|
||||||
|
| `%` | Remainder | integers |
|
||||||
|
| `&` | Bitwise AND | integers |
|
||||||
|
| `|` | Bitwise OR | integers |
|
||||||
|
| `^` | Bitwise XOR | integers |
|
||||||
|
| `&^` | Bit clear (AND NOT) | integers |
|
||||||
|
| `<<` | Left shift | integer << unsigned integer |
|
||||||
|
| `>>` | Right shift | integer >> unsigned integer |
|
||||||
|
|
||||||
|
Comparison Operators
|
||||||
|
| Symbol | Operation |
|
||||||
|
|:---------:|:-------------:|
|
||||||
|
| `==` | Equal |
|
||||||
|
| `!=` | Not equal |
|
||||||
|
| `<` | Less |
|
||||||
|
| `<=` | Less or equal |
|
||||||
|
| `>` | Greater |
|
||||||
|
| `>=` | Greater or equal |
|
||||||
|
|
||||||
|
Logical Operators
|
||||||
|
| Symbol | Operation |
|
||||||
|
|:---------:|:-------------:|
|
||||||
|
| `&&` | Conditional AND |
|
||||||
|
| `||` | Conditional OR |
|
||||||
|
| `!` | NOT |
|
||||||
|
|
||||||
|
[Return to Summary](#summary)
|
||||||
|
|
||||||
|
<hr/>
|
||||||
|
|
||||||
|
## Conditional Statements
|
||||||
|
|
||||||
|
```go
|
||||||
|
// If / Else
|
||||||
|
i := 1
|
||||||
|
|
||||||
|
if i > 0 {
|
||||||
|
// Condition is True! i is greater than zero
|
||||||
|
} else {
|
||||||
|
// Condition is False! i is lower or equal to zero
|
||||||
|
}
|
||||||
|
|
||||||
|
// Else if
|
||||||
|
i := 1
|
||||||
|
|
||||||
|
if i > 0 {
|
||||||
|
// Condition is True! i is greater than zero
|
||||||
|
} else if i > 0 && i < 2 {
|
||||||
|
// Condition is True! i greater than zero and lower than two
|
||||||
|
} else if i > 1 && i < 4 {
|
||||||
|
// Condition is True! i greater than one and lower than four
|
||||||
|
} else {
|
||||||
|
// None of the above conditions is True, so it falls here
|
||||||
|
}
|
||||||
|
|
||||||
|
// If with short statements
|
||||||
|
i := 2.567
|
||||||
|
|
||||||
|
if j := int(i); j == 2 {
|
||||||
|
// Condition is True! j, the integer value of i, is equal to two
|
||||||
|
} else {
|
||||||
|
// Condition is False! j, the integer value of i, is not equal to two
|
||||||
|
}
|
||||||
|
|
||||||
|
// Switch
|
||||||
|
text := 'hey'
|
||||||
|
|
||||||
|
switch text {
|
||||||
|
case 'hey':
|
||||||
|
// 'Hello!'
|
||||||
|
case 'bye':
|
||||||
|
// 'Byee'
|
||||||
|
default:
|
||||||
|
// 'Ok'
|
||||||
|
}
|
||||||
|
|
||||||
|
// Switch without condition
|
||||||
|
value := 5
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case value < 2:
|
||||||
|
// 'Hello!'
|
||||||
|
case value >= 2 && value < 6:
|
||||||
|
// 'Byee'
|
||||||
|
default:
|
||||||
|
// 'Ok'
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
[Return to Summary](#summary)
|
||||||
|
|
||||||
|
<hr/>
|
||||||
|
|
||||||
|
## Loops
|
||||||
|
|
||||||
|
```go
|
||||||
|
// Golang only has the for loop
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
// i
|
||||||
|
}
|
||||||
|
|
||||||
|
// The first and third parameters are ommitable
|
||||||
|
// For as a while
|
||||||
|
i := 0;
|
||||||
|
|
||||||
|
for i < 10 {
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
|
||||||
|
// Forever loop
|
||||||
|
for {
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
[Return to Summary](#summary)
|
||||||
|
|
||||||
|
<hr/>
|
||||||
|
|
||||||
|
## Functions
|
||||||
|
```go
|
||||||
|
// Functions acts as a scoped block of code
|
||||||
|
func sayHello() {
|
||||||
|
// Hello World!
|
||||||
|
}
|
||||||
|
sayHello() // Hello World!
|
||||||
|
|
||||||
|
// Functions can take zero or more parameters, as so return zero or more parameters
|
||||||
|
func sum(x int, y int) int {
|
||||||
|
return x + y
|
||||||
|
}
|
||||||
|
sum(3, 7) // 10
|
||||||
|
|
||||||
|
// Returned values can be named and be used inside the function
|
||||||
|
func doubleAndTriple(x int) (double, triple int) {
|
||||||
|
double = x * 2
|
||||||
|
triple = x * 3
|
||||||
|
return
|
||||||
|
}
|
||||||
|
d, t := doubleAndTriple(5)
|
||||||
|
// d = 10
|
||||||
|
// t = 15
|
||||||
|
|
||||||
|
// Functions can defer commands. Defered commands are
|
||||||
|
// runned in a stack order after the execution and
|
||||||
|
// returning of a function
|
||||||
|
var aux = 0
|
||||||
|
|
||||||
|
func switchValuesAndDouble(x, y int) {
|
||||||
|
aux = x
|
||||||
|
defer aux = 0 // cleaning variable to post use
|
||||||
|
x = y * 2
|
||||||
|
y = aux * 2
|
||||||
|
}
|
||||||
|
|
||||||
|
a, b = 2, 5
|
||||||
|
switchValuesAndDouble(2, 5)
|
||||||
|
|
||||||
|
// a = 10
|
||||||
|
// b = 4
|
||||||
|
// aux = 0
|
||||||
|
```
|
||||||
|
|
||||||
|
[Return to Summary](#summary)
|
||||||
|
|
||||||
|
<hr/>
|
Loading…
Reference in New Issue