From the course: Learning Go

Create a Hello World application - Go Tutorial

From the course: Learning Go

Create a Hello World application

- [Tutor] Once you've installed the Go development tools, you can create as many Go applications as you like. Each application should be in its own folder. I'm going to create a very simple, Hello World application. So I'll go to my Explorer window and right click and choose new folder. I'll give it a name of Hello, whatever name you give the folder will become the name of the resulting compiled application. The folder names should be all lowercase with no spaces. Now I'll create a source code file within that folder and I'll name it, main.go. There's some rules around this filename, the .go extension is required for all source code files. The name of the file before the file extension DOE could be anything you like, but once again, it should be all lowercase and no spaces. In order to serve as the startup code for an application, this file must be a member of a package called main. So I'll add that code at the top of the file. Next I'll need imports for any packages I'm going to use in my application. I'll start with the keyword import and if I'm only going to be using one package, I can do it all on a single line. I'm going to be using a package called, fmt for formatting. If I wanted to use more than one package, I could wrap this code in parentheses and I'll apply the correct formatting to this. And now I could add additional packages underneath the first one. I've already added this file to the main package and the other requirement for it to serve as a start-up file is that there must be a function named, main. All functions in go, start with the func keyword and the function names start with a lowercase character if they're private to this file or start with an uppercase character, if they're public. I'll add a pair of braces and then I'll call fmt and I'll call one of the functions from this package, Print ln. And I'll pass in a string of, Hello from Go. I'll save my changes by pressing Command + S on Mac or Control + S on windows, and a couple of things will happen. First of all, in the background the Go compiler examines the file and determines whether there are any syntax errors. Also, if you have any unused imports at the top of the file, you may see them go away, that's normal behavior. Now to run the application, I have a few different options. I'll go to the menu and choose, Run, Run Without Debugging. And the Debug Console appears over here. You'll also see something called a Problems window but this code doesn't have any problems so I'm going to shrink it down and the Debug Console floats over to the left. So that's one approach to running the application, running it completely from within Visual Studio Code. But now let's go to a terminal. To go to a terminal that starts in the directory where this file is stored, right click on the file in the Explorer window and choose, Open In Integrated Terminal, then list the contents of your directory. On Mac and Linux, you would use LS and on Windows you'd use DIR and I see that I'm in the correct directory. Here's a couple of ways to run this application from the terminal, I'll type go and then the run command. And then I could pass in the file name like this and I would see the output of the application. But there's a slightly shorter way, this time I'll type it go run and simply pass in the dot character. Within a single application, only one source code file can have a main function. So all you need to do is designate which folder you're working with and the compiler figures out everything else. And once again, I see the output. I'm now running the application depending on the runtime that's installed on my computer. But if I want to package the runtime and build a binary file that I can distribute, I can do this, Go build., I don't see any output, but now if I list the contents of this directory I see a new file named, hello. It has the same name as the folder in which it's contained. If I then type, ./hello, I'm running the compiled version. And you may notice that it's a lot faster than running that application with Go run. This is now a compiled binary file, specifically designed to work on my operating system. If I take that file and copy it over to Windows, it isn't going to work. And if you're working on Windows, instead of seeing just, hello, you'll see, hello.exe. It's also possible to compile for other operating systems, but by default you'll get a binary for your operating system. So if that's all working, you're ready to use Visual Studio Code to create your own Go-based command line applications.

Contents