-
Notifications
You must be signed in to change notification settings - Fork 12
kimschles edited this page Jul 12, 2019
·
5 revisions
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
)
- syntax:
var name type
- example:
var name string
-
%T
prints the type -
int
's default value is 0!
- 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:
- 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
-
:=
- for example:
name, age := "Kim", 34
- no
var
- for example:
- Only works for newly declared variables
(come back to this)
- 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(){}
- 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)
}
func rectProps(length, width float64)(area, perimeter float64) {
area = length * width
perimeter = (length + width) * 2
return //no explicit return value
}
_
- 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)
}
- 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
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"))`