Skip to content
kimschles edited this page Jul 12, 2019 · 5 revisions

golang

package main

import "fmt"

func main () {
    fmt.Println("Hello World")
}
  • Every go file starts with a package name statement. This forces you to write modular code.
  • import "fmt" means to import the fmt 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!

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"

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"
  • type inference works here, too

Short hand declaration

  • :=
    • for example: name, age := "Kim", 34
    • no var
  • 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"))`
Clone this wiki locally