Key features of the Go language: - Simplicity - Strong and statically typed - Fast compile times - Garbage collected, AKA you do not need to manage your own memory - Built-in concurrency - Compile to standalone binaries - Used for servers and web-applications - Excellent community (golangbridge.org)
Variable Declaration
//// The 3 ways of declaring variables // #1: Blank declaration var i int i = 42 // #2: Compacted declaration var i int = 42 // #3: Shorthand declaration i := 42
NOTE: You cannot use the colon-equal syntax at the package level, you have to use the full syntax
// NOPE showSeason := 20 // YES var showSeason int = 20 /* TIP: declare variables like how you would read it. "You declare a variable named i with the type of integer". */
If you have a bunch of variables of the same datatype at the package level, you can group them up in a block and declare them at once. This makes the code look cleaner.
// Dirty code without block declaration var ygoName string = "Xie" var deckName string = "Manipulstring" var charAge string = "???" // Cleaner code with block declaration var ( ygoName string = "Xie" deckName string = "Manipulstring" charAge string = "???" )
Variable Re-declaration & Shadowing
You cant re-declare variables, but you can shadow them.
You have to use variables you declared, otherwise you get yelled at.
Three Different Levels of Visibility:
1. Variables declared with a lowercase first-letter, which are visible to the package 2. Variables declared that are capitalized, which is visible globally 3. Variables declared inside blocks are block-scoped, and they can only be visible in that block of code NOTE: Use single-letters for the names of short-lived variables; longer names for long-lived variables (using camelCase).
Type Conversion:
- destinationType(variable) - Use strconv package for converting things to strings - You have to be explicit in your conversion. Go will not assume type conversion for the fear of losing valuable data
aeae
