From the course: Learning Go

Explore basic Go syntax - Go Tutorial

From the course: Learning Go

Explore basic Go syntax

- [Instructor] Every programming language has certain basic syntax rules, and you need to know these rules to be effective with that language. Here are some critical syntax rules in Go. Go is case sensitive. So identifiers such as function, variable and type names, have to be spelled exactly the way you see them in the documentation. Most variables and package names are lower and mixed case, but methods, which are functions that belong to types and fields, which are variables that are members of types frequently have initial uppercase characters. That has a special meaning in Go. An initial uppercase character, means that that symbol is exported. And that's the equivalent of the keyword public in other languages like Java and C sharp, a lowercase initial character means that the field or method isn't exported and isn't available to the rest of the application, or you could say that it's private. Another important aspect of Go, is that it tries to reduce the amount of typing you have to do as a developer. And one of the ways it does this is to eliminate semi-colons from the ends of lines. This bit of code is declaring a variable an array of strings, and then setting the values of the items in the array. Notice that there aren't any semi-colons at the end of any of these lines of code. Now, interestingly, the language specification says that semi-colons are supposed to be there, but you don't have to type them. And that's because the lexer, which is the software component that parses your code and analyzes it, adds the semi-colons during the compilation process as needed. In order to accomplish this, Go is sensitive to white space. That is line feeds, tabs, and so on. There is a fairly simple rule that dictates when the semi-colons are added for you. If a statement is determined to be complete and the lexer encounters a line feed, that means it's the end of the statement, and you don't need to add the semi-colon yourself. But that also means, that you can't always add line feeds freely or break up statements with extra line feeds as you might do in other languages, because in certain circumstances that can be misinterpreted by the lexer. As with other C style languages code blocks. That is multiple lines of code that are intended to be grouped together are wrapped with brace characters. Here's an example, I'm declaring a variable named some, and then I moved being from zero to 10 and I'm incrementally adding the value each time. And then I'm outputting that value using a function called print LN or print line. That's a member of a package called FMT for format, and it outputs the value of 45. In a four statement, the code that you want to iterate over is wrapped between those two braces now because of the rules of how the lexer analyzes new lines, the first brace and a code block must be on the same line as any proceeding statement. Unlike other C style languages that aren't sensitive to white space, in Go, you don't have the freedom to drop that beginning brace down to the next blank line. Unlike in Java, where every method is a member of a class, Go has a set of built-in functions that are always available in your code without having to import anything. These functions are members of a special package named builtin. The Go compiler assumes that the builtin package is always imported. You'll find functions like len for length, panic, to stop execution and displaying an error message, and recover to manage the behavior of a panicking routine. You can learn about these and other built-in functions and types at this page in the online documentation, @golang.org/pkg/builtin.

Contents