Table of contents
Child pages
Reviews of the language
- 2015.04.21 - Evan Miller - Four Days of Go
- 2017.08.22 - Michal Konarski - 5 things about programming I learned with Go
- HN discussion
- It is possible to have both dynamic-like syntax and static safety
- Go is not an object-oriented language. But it does have interfaces. And they are pretty much the same as these you can find in Java or C++. They have names and define a set of function signatures.
- Then we have Go’s equivalent of classes - structs. Structs are simple things that bundle together attributes.
- We can add a function to a struct.
- Go uses a concept called “automatic interface implementation”. A struct containing all methods defined in the interface automatically fulfills it. There is no implements keyword. Isn’t that cool? A friend of mine even likes to call it “a statically typed duck typing”. Thanks to that feature and type inference that allows us to omit the type of a variable while defining, we can feel like we’re working in a dynamically typed language. But here we get the safety of a typed system too.
- It’s better to compose than inherit
- if we want to mitigate the risk of getting lost inside the dark forest of code complexity we need to avoid inheritance and prefer composition instead.
- Go doesn’t support inheritance at all.
- There is a feature called embedding. Every method in an embedded interface is accessible directly on the struct that the interface is embedded in.
- Channels and goroutines are powerful way to solve problems involving concurrency
- Don’t communicate by sharing memory, share memory by communicating.
- Instead of using locks to control access to a shared resource, Go coders can simply use channels to pass around its pointer. Then only a goroutine that holds the pointer can use it and make modifications to the shared structure.
- There is nothing exceptional in exceptions
- there is nothing exceptional in exceptions. They are usually just one of possible return values from a function....it’s good to think about exceptions like they were regular return values. Don’t pretend that they just won’t occur.
- f, err := os.Open("filename.ext")
- Quora - Why did Google create the Go language? Isn't Python good enough?
"Python is a dynamically typed language, and as such it can present, er, challenges for working on large programs, in large teams. A quick example, is that if you make a function in Python to call, and call it from a few places, you’ll find that if you change the number of parameters, or types of parameters that the function takes, there is no compile time error, only a runtime error. Now that’s no big deal on a small program of only a few hundred or even few thousand lines, but once you go up to hundreds of thousands of lines, or millions of lines, working with hundreds of other people, it’s a major problem.Statically typed language like Go turn that runtime error into a compile time error, and it’ll point out each and every time that function is called with the wrong parameters. This is a huge difference, when working on medium or large scale projects."
"Python is very good at making programming simple and easy, but it suffers a performance hit compared to compiled languages. Compiled languages such as C or C++ are very fast, but they're not as simple and easy to use as Python. Go aims to be almost as easy to use as Python while being compiled, and almost as fast as traditional compiled languages. It's also very good at concurrency by design, which is its main strength."
- Go FAQ - Why are you creating a new language?
- We wanted the ease of programming of an interpreted, dynamically typed language...
- ...and the efficiency and safety of a statically typed, compiled language.
- Finally, working with Go is intended to be fast: it should take at most a few seconds to build a large executable on a single computer.
Learning resources
- Udemy - A search for "golang"
- Looks like there are a couple of good options...
- How I Start - Go
Go: The Complete Developers Guide
- https://www.udemy.com/go-the-complete-developers-guide/learn/v4/content
- This was the highest-rated intro to Go on Udemy.
Section 1: Getting Started
1. How to Get Help
- No video, just text.
- There are three ways to get help:
- Post in the Q&A discussion
- Email me: <email address redacted>
- Message me on Udemy.
2. Link to Completed Code
- No video, just text.
- You can find the completed version of all the examples in this course here: https://github.com/StephenGrider/GoCasts
3. Environmental Setup
- We are going to install Go, then we'll install VSCode (an IDE), then we'll configure VSCode, then we'll write some code.
- He highly recommends trying VSCode because it has the best integration with Go.
- To install Go, go to golang.org/dl
- Just keep hitting "Next" for the installer, there's nothing we need to configure.
4. VSCode Installation
- To verify Go was installed correctly, start a terminal / command prompt window and type "go" and hit Enter. You should see a help message show up on the screen.
- To install VSCode, go to code.visualstudio.com
5. Go Support in VSCode
- VSCode doesn't start out with any support for Go.
- To add this support, go to View → Extensions, and search for "go".
- Download the extension with the title "Go", the description "Rich Go language support..." and over 1 million downloads.
- Restart VSC.
- Make sure you have a code editor window open (do File → New File if you don't).
- In the bottom right corner, click "Plaintext" and switch it to "Go", and in the same place where "Plaintext" showed up you'll see an error saying "Analysis tools missing". Click the error message and then click "Install" on the dialogs that pop up.
Section 2: A Simple Start
6. Boring Ol' Hello World
- We're going to start with a simple "Hello World" example in this video but in the next few videos we're going to do a deep dive on how Go is working behind the scenes.
- He goes to File → Open and creates a new folder named "helloworld" to house this project.
- NW: In my editor I didn't have an "Open" option, just "Open File" and "Open Folder", so I chose the latter.
- You should see the folder show up in the left sidebar (the "Explorer tab").
- He clicks a little "Add file" button next to the name of his project in the Explorer tab and names it "main.go".
- He writes out the code and says we'll discuss each line in-depth in the next few videos:
- package main
import "fmt"
func main() {
fmt.Println("Hi there!")
} - He says you must use double-quotes, not single quotes, in both the import statement and the Println statement.
- package main
7. Five Important Questions
- He studied the Hello World program ahead of time and came up with five basic questions we can ask that will give us a good sense of what the program is doing:
- How do we run the code in our project?
- What does 'package main' mean?
- What does 'import "fmt"' mean?
- What's that 'func' thing?
- How is the main.go file organized?
- He then starts answering question 1.
- He switches to a terminal navigated to his project folder.
- He runs "go run main.go"
- NW: When I tried this I ran into a minor problem in that my code had not been auto-saved like it is in PyCharm, so it didn't work until I figured out that that was happening.
- He shows several other go CLI commands that we'll be using.
- "go run" is used to compile and immediately execute one or two files, without generating an executable file.
- "go build" compiles, but does not execute, go files. It will generate an executable file.
- "go fmt" formats all of the code in each file in the current directory.
- "go install" compiles and 'installs' a package.
- "go get" downloads the raw source code of someone else's package.
- "go test" runs any tests associated with the current project.
8. Go Packages
- We're going to talk about what "package" means and why we chose the name "main".
- A package can be thought of as a project or workspace.
- A package can have many Go source code files in it.
- Every file that belongs to a package must have its first line declare the package it is a part of.
- As for why we chose "main":
- There are two types of packages: executable packages and reusable packages.
- Executable packages generate an executable we can run.
- Reusable packages are used as helpers / libraries.
- The name of the package that you use determines whether you're creating an executable or reusable package.
- In particular, the word "main" for a package is what designates a package as executable.
- Any other name for a package will create a reusable package.
- An executable package must have a function called "main".
9. Import Statements
- The import statement is used to give our package access to code contained in another package.
- "fmt" is a standard library included with Go. It stands for "format". It's used for printing out information.
- We can import both standard packages that are included with Go as well as packages that are not official and have been authored by other individuals.
- We can learn more about the standard packages by going to golang.org/pkg
- We are going to be looking at these official docs a lot, because a lot of learning Go is learning how these standard packages work.
10. File Organization
- So, what's that 'func' thing?
- 'func' is short for "function". Functions in Go work just like functions in other languages.
- How is the main.go file organized?
- It's always the same pattern in every file:
- At the very top we have a "package ..." declaration.
- Then we'll import other packages we need.
- Then we'll declare functions.
Quiz 1
- What is the purpose of a package in Go?
- What is the special name we use for a package to tell Go that we want it to be turned into a file that can be executed?
- The one requirement of packages named "main" is that we...
- Why do we use "import" statements?
11. How to Access Course Diagrams
- No video, just text.
How to use the course diagrams:
Go to https://github.com/StephenGrider/GoCasts/tree/master/diagrams
Open the folder containing the set of diagrams you want to edit
Click on the ‘.xml’ file
Click the ‘raw’ button
Copy the URL
Go to https://www.draw.io/
On the ‘Save Diagrams To…’ window click ‘Decide later’ at the bottom
Click ‘File’ -> ‘Import From’ -> ‘URL’
Paste the link to the XML file