Go Syntax
Here you will learn some of the basic Go syntaxes.
Go statement
Each Go statement ends with a line break. A line break denotes the termination of a statement in Go. You don't need to put semi-colon. It automatically adds an invisible semi-colon at the end of the statement.
The following two statements are considered separate statements in Go.
fmt.Println("Hello")
fmt.Println("Welcome to Go programming!")
Comments
Comments are lines in a program which is ignored (not executed) by the compiler. Comments are added to include documentation and to make the code more readable.
To comment out multiple statements, wrap statements between /*
and */
, as shown below.
/*
This is a block comment in Go
You can group many lines of comments together
*/
Use double slashes //
to comment a single line statement.
//This is a single-line comment.
You can also use the double slashes for multiple lines as shown below:
//This is a multiline comment
//Use two slashes to comment multiple lines
//This is a valid style
Functions
A function in Go can be defined as a group of statements that performs a specific task and returns an output. A function is enclosed within a package and a package can have multiple functions.
In Go, a function is declared using the func Keyword followed by the function name, open/close parenthesis, and open curly braces as shown below. The closed curly brace denotes the end of the function.
func Display() {
//display message on console
fmt.Println("Learn Go programming with TutorialsTeacher")
}
// Greet() function returns string
func Greet(name string) string {
// declare a variable
message := fmt.Sprintf("Hello, %v. Welcome!", name)
return message // return string
}
Note: A function can accept any number of arguments passed to the function within the parenthesis after the function name declaration
Packages
Go language code is written in packages. Packages can be defined with the keyword package
. The following defines a package called main
that contains the main()
function.
package main
Import "fmt"
func main() {
fmt.Println("Welcome to Go programming")
}
In Go, the main package will contain the main()
function. A Go program starts executing at the main package that compile as an executable. The main()
function must not accept any arguments and does not return any value.
The import keyword is used to import other packages into your program. The fmt package is used to format strings, values, input/outputs. It also prints to the terminal.
The import statement import "fmt"
imports the fmt
module where the Println()
function is used to format the output and writes to the standard output.
Data Types
The Go programming language contains the following data types:
int | int8 | int16 | int32 | int64 | uint |
uint8 | uint16 | uint32 | uint64 | uintptr | channels |
float32 | float64 | complex64 | complex128 | boolean | string |
array | struct | pointers | slice | maps |
Character and Strings
A character or a single letter is enclosed in single quote '
e.g. 'a'.
A string or a sentence is enclosed in double-quotes "
e.g. "Hello World".
Scope and Curly Braces
Open and close curly braces are used to define the scope in Go.
func main(){
var greet = "Hello World"
}
The variable greet is of local scope to the main function and can be used only inside the function main.
In Go, the open curly braces cannot be placed in a new line on their own as the Go compiler will insert a semicolon and this breaks the code.
The following shows the correct way of using open curly braces.
package main
import "fmt"
//correct style
func main() {
fmt.Printf("Hello, World!")
}
//wrong style
func main()
{
fmt.Printf("Hello, World!")
}
Any variable defined outside the main()
function is a global variable and can be used by other functions in that script.
Keywords
break | case | chan | const | continue |
default | defer | else | fallthrough | for |
func | Go | Goto | if | import |
interface | map | package | range | return |
select | struct | switch | type | var |
Identifiers
Identifiers are names used to identify things like variables, functions, types, structs, etc.
- An identifier can start with an uppercase, lowercase, or underscore but cannot start with a numeric value, e.g. MyVar, myvar, _myVar, _Myvar are valid names.
- Identifiers are case-sensitive e.g. MyVar and myVar are treated as different names.
- Cannot use special characters in identifiers, e.g. #&myVar, My@Var are invalid
- Should not contain a any reserved keyword, e.g. var, type, func are invalid identifier names.