Filenames, Keywords and Identifiers

Filenames, Keywords and Identifiers

File Naming Conventions

In Go, source code is stored in files with the .go extension. These filenames follow a simple convention—using only lowercase letters. For example, a file might be named educative.go. When you need to use multiple words in the name, the words are separated by underscores, like db_connection.go. It’s important to note that Go filenames can’t contain spaces or special characters beyond the underscore.

As for the code inside the file, there’s no strict limit on how long each line can be, though keeping lines readable is always a good practice.


Keywords in Go

A keyword in any programming language is a reserved word that has a special meaning and can’t be used for anything else, like naming variables or functions. Go has 25 keywords that you'll encounter regularly when writing code.

Here's a list of all Go keywords:

Go Keywords

These words are the backbone of Go’s syntax, so you’ll want to familiarize yourself with them as you continue learning.


Identifiers in Go

An identifier is a name you assign to different elements in your code, such as variables, functions, structs, and so on. Like many other programming languages, Go is case-sensitive, meaning that MyVariable and myvariable would be treated as two distinct identifiers.

Identifiers in Go must start with either a letter (from the Unicode set, so you can use more than just English letters) or an underscore (_). After the first character, you can include any combination of letters and digits.

Some examples of valid identifiers:

  • X56

  • group1

  • _x23

  • i

  • өԑ12

But not everything is valid. Here are some examples of invalid identifiers:

  • 1ab (since it starts with a number)

  • case (because it's a Go keyword)

  • a+b (operators aren’t allowed in identifiers)

In addition to keywords, Go also has a set of 36 predeclared identifiers, which include the names of basic types and built-in functions. Here’s a list of them:

Predeclared Identifiers


The Blank Identifier

Go includes a special identifier: _, called the blank identifier. You can use it in variable declarations and assignments, but it discards the value assigned to it. It’s useful when you need to ignore certain return values or data in your code. For example, if a function returns two values but you only need one, you can assign the unwanted value to _.

Anonymous

Sometimes it is possible that even functions have no name because it is not really necessary at that point in the code and not having a name even enhances flexibility. Such functions are called anonymous.

The basic structure of a Go program

Programs consist of keywords, constants, variables, operators, types and functions. It is also important to know the delimiter and punctuation characters that are a part of Golang.

The following delimiters are used in a Go program:

  • Parentheses ()

  • Braces {}

  • Brackets []

The following punctuation characters are used in a Go program:

  • .

  • ,

  • ;

  • :

  • ...

The code is structured in statements. A statement doesn’t need to end with a ; (like it is imposed on the C-family of languages). The Go compiler automatically inserts semicolons at the end of statements. However, if multiple statements are written on one line (a practice which is not encouraged for readability reasons), they must be separated by ;.


That’s it about the basic structure and elements of Golang. In the next lesson, we’ll see a basic component of the Go program.