Writing a "Hello, World!" program.¶
Objective¶
Write a program which prints "Hello, World!" on the screen.
Structure¶
Let's create a folder called 'code' anywhere on your machine and we will put all our Go code in it. Inside the 'code' folder lets create one more folder for basic tutorials
1 |
|
For our first program create a new folder '01_hello_world'
1 |
|
And lets create a file 'hello_world.go' in it, finally the structure would look like this:
1 |
|
Code¶
Write the code as shown below, while you can simply copy and paste, its better if you write everything on your own.
Don't worry if you code won't work, only then copy and paste the code
Hello World
1 package main
2
3 import "fmt"
4
5 func main() {
6 fmt.Println("Hello, World!")
7 }
Running your code¶
Open your terminal and navigate to our folder
1 |
|
Once in the folder type the following command
1 |
|
Build¶
Instead of just running let's try to build our code by compiling it to binary.
Instead of "go run" type the following command
1 |
|
If you are running on a unix based system including mac OSx, you can run the binary by typing
1 |
|
Output¶
If there are no errors, you should get the output as:
Output
1 |
|
If for some reason your code isn't working, checkout the golang playground or github links in the following section.
Github¶
That's it, Congratulations ! You just wrote your first Go program.
Golang Playground¶
Golang has a online sandbox environment for running your Go programs, which can be accessed on Golang Playground
I will be posting all the playground links for all the code we write, this way you can run them online and compare with your code.
Next¶
If you haven't understood anything what you wrote, no worries, you aren't expected to understand it just yet !
In the next section we will understand everything line-by-line.