go - kimschles/schlesinger-knowledge GitHub Wiki
golang
golangbot Hello World
package main
import "fmt"
func main () {
fmt.Println("Hello World")
}
- Every
go
file starts with apackage name
statement. This forces you to write modular code. import "fmt"
means to import thefmt
package, short for formatted. The package allows the developer to use formatted i/o.func main
is where the program begins. The main function always lives in the main package.fmt.Println
writes texts to standard output (Stdout
)
golangbot Variables
- syntax:
var name type
- example:
var name string
%T
prints the typeint
's default value is 0!
Type Inference
- if you initialize the value of your variable, you can skip the type declaration
- For example:
var name = "Kim"
does not need the type "string"
- For example:
Multiple Variables
- If they have the same type, you can separate variable names with a comma during the declaration
- For example:
var first_name, last_name string = "Kim", "Schlesinger"
- For example:
- type inference works here, too
Short hand declaration
:=
- for example:
name, age := "Kim", 34
- no
var
- for example:
- Only works for newly declared variables
Types
(come back to this)
Functions
- golang syntax for functions:
func functionname(parametername type) returntype {
\\ function body
}
- parameters and parametertypes are optional, so a function can look like this:
func functionname(){}
Multiple Return Values
- a function that takes the length and width of a rectangle and returns the area and perimeter
package main
import "fmt"
func runCacluation(length, width float64)(float64, float64){
var area = length * width
var perimeter = (length + width) * 2
return area, perimeter
}
func main(){
area, perimeter ;= runCalcuation(10.8, 5.6)
fmt.Printf("Area %f Perimeter %f", area, perimeter)
}
Named Return Values
func rectProps(length, width float64)(area, perimeter float64) {
area = length * width
perimeter = (length + width) * 2
return //no explicit return value
}
Blank Identifier
_
- A way to discard a parameter you aren't using.
package main
import (
"fmt"
)
func rectProps(length, width float64) (float64, float64) {
var area = length * width
var perimeter = (length + width) * 2
return area, perimeter
}
func main() {
area, _ := rectProps(10.8, 5.6) // perimeter is discarded
fmt.Printf("Area %f ", area)
}
Packages
- Write small, modular code with packages
- Every executable go app has a main function
- the main function is the entry point for execution
- the main function lives in the main package
go install
- A good practice: package names match the directory they live in
- Capitalize function names in packages (Average instead of average)
- You can alias pacakge names in the import statement:
import m "golang-book/chapter11/math
Conditional Statements
if condition {
} else if condition {
} else condition {
}```
or
```go
if statement; condition {
}```
Example of the latter:
```go
if name := "Kim"; name == "Kim" {
fmt.Println("your name is", name)
} else {
fmt. Println("I do not know your name")
}
## Misc.
* `%s` is for string interpolation
* `rune` is a 32-bit integer value
* `template` directory
* a `struct` is a data structure that holds information that will be displayed in our HTML file
* `http.FileServer(http.Dir("static"))`