Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Go is a systems programming language designed for speed and simplicity. You can compile a single Go program into executables that will run on Windows, MacOS, or Linux, without making any changes to your code. And your users won't have to install interpreters or virtual machines or anything. You just give them a single executable file, and they're ready to run your software.
Goals for Go:
- Fast builds
- Fast execution
- Concurrency
- Garbage collection
A team at Google found that a build of a Go program did 50 times fewer file includes than a comparable C++ program.
Hello, world
Let's try out your first Go program. We'll use a time-honored example and write a program that prints "Hello, world!". Launch the workspace attached to this video, and create a new source code file named "hello.go". Then type in the following code. Don't worry about memorizing everything we're doing right now; we'll be recapping all of this in upcoming videos.
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
Now, go to the workspace console, and type go build hello.go
. This will take your source code and compile it into an executable program. Now, if we run ls -l
(that's "list files in long format") in the console, we'll see a new executable binary file named just hello
without an extension. If we now type ./hello
(which runs an executable file in the currect directory named hello
, we'll see our message: "Hello, world!"
If you just want to try out some Go code without storing an executable binary file, there's a shortcut you can use. Just type the command go run hello.go
. Your program will run immediately. This takes a moment, because Go still has to compile the code before running it, but it's convenient because you don't have to make a bunch of executable files before you're even sure if your program is working correctly. We'll be using the go run
command for the rest of this course.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up