Join David Gassner for an in-depth discussion in this video Creating and compiling a hello world application, part of Learning Go.
- So now it's time for the traditional "hello world" application. The first application you always write in every new programming language. I'm working in the Exercise Files folder in Visual Studio Code. And I'm going to right-click on the 01_06 folder. And create a new file. I'll name the file "helloworld" with the file extension ".go". Every source code file in Go has the .go file extension. Now, the first statement in the file is a package declaration.
It starts with the keyword "package". Your startup file always has a package of main. It's an identifier, not a string, so you don't put quotes around it. Also the package name doesn't have to match the name of the directory, or the source code file, as is the case in, say, Java. So in this example, my directory is just a number, the source code file is helloworld.go, and the package is main. And that's just fine. When we get into complete workspaces later on, I'll show you some more traditional ways of naming folders.
But this will work for this example. Next, import the packages that you want to use in your applications. The Go libraries are organized as packages. As I mentioned previously, there's a package named "built in", that has functions and constants that are always available to your code, but you have to explicitly import everything else. I'll type the word "import", and for this application I only need one package, named "fmt". When you import packages, you wrap the package names in quotes.
Now, if you're going to be importing more than one package, you can do it like this. Put in a pair of parentheses and then stack the package names on top of each other. There's no actual package named "package2", so this is just an example, but this is what it would look like. I'll get rid of that dummy package and go on to the next step. The main package should have a function named main. Each function starts with the key word "func", F-U-N-C.
Then the name of the function. The main function, which will be called automatically upon application start-up, is all lower case, and doesn't have any arguments. I'll put in the braces for the function body. And then I'll use this code to output "hello world". I'll start with the package name, fmt. Then after a dot, the name of the function or method I want to call. The method I'm going to call is Println, with an upper-case P.
It's available from the fmt package because it's been exported. And it's been exported because of the initial upper-case character, the P in Println. Then I'll pass in a single string argument. You wrap your strings in double quotes. And I'll say "Hello from Go!" I'll save my changes, I pressed Control + S in Windows, on Mac you would press Command + S. Then I'll go back to my file in the file listing, right-click, and choose "Open in Command Prompt".
Then I'll type go, run, then I'll press tab to select the file in the folder, and press Enter or Return, and there's the result. Here's an example of importing a second package. I'll go up to the import statement, and I'll import a package named "strings". I'll be talking about the Strings package later in the course. But it has a bunch of functions that let you manipulate strings. Then I'll go down here to my main code.
And once again I'll call fmt.Println. And this time I'll call "strings.ToUpper". And I'll pass in a string of "Hello really loud!" I'll save and run again. And there's the result. So that's the simplest sort of Go application. It just outputs simple strings to the Console. But it gives you a sense of the basic structure of a Go package, including the package declaration, the import, and the main function.
- Installing Go tools
- Creating and compiling a Go workspace
- Exploring variables, constants, and types
- Storing ordered and unordered values
- Grouping related values in structs
- Programming conditional logic and loops
- Defining and calling functions
- Handling errors
- Working with files
- Creating a simple HTTP server