Go Practices 101
Theme: dark/light
Go Optimizations 101,
Go Details & Tips 101
and Go Generics 101
are all updated for Go 1.24 now.
The most cost-effective way to get them is through this book bundle
in the Leanpub book store.
TapirMD - a powerful, next-generation markup language that simplifies content creation (much more powerful than markdown).
You can experience it online
here.
This article will introduce keywords and identifiers in Go.
Keywords are the special words which help compilers understand and parse user code.
Up to now (Go 1.23), Go has only 25 keywords:
break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var
They can be categorized as four groups:
-
const
, func
, import
, package
, type
and var
are used to declare all kinds of code elements in Go programs.
-
chan
, interface
, map
and struct
are used as parts in some composite type denotations.
-
break
, case
, continue
, default
, else
, fallthrough
, for
, goto
, if
, range
, return
, select
and switch
are used to control flow of code.
-
defer
and
go
are also control flow keywords, but in other specific manners. They modify function calls, which we'll talk about in
this article.
These keywords will be explained in details in other articles.
An identifier is a token which must be composed of Unicode letters, Unicode digits and _
(underscore), and start with either an Unicode letter or _
. Here,
-
Unicode letters mean the characters defined in the Letter categories Lu, Ll, Lt, Lm, or Lo.
-
Unicode digits mean the characters defined in the Number category Nd of The Unicode Standard.
keywords can not be used as identifiers.
Identifier _
is a special identifier, it is called blank identifier.
Later we will learn that all names of types, variables, constants, labels, package names and package import names must be identifiers.
An identifier starting with an
Unicode upper case letter is called an
exported identifier. The word
exported can be interpreted as
public in many other languages. The identifiers which don't start with an Unicode upper case letter are called non-exported identifiers. The word
non-exported can be interpreted as
private in many other languages. Currently (Go 1.23), eastern characters are viewed as non-exported letters. Sometimes, non-exported identifiers are also called unexported identifiers.
Here are some legal exported identifiers:
Player_9
DoSomething
VERSION
Ĝo
Π
Here are some legal non-exported identifiers:
_
_status
memStat
book
π
一个类型
변수
エラー
And here are some tokens which are illegal to be used as identifiers:
// Starting with a Unicode digit.
123
3apples
// Containing Unicode characters not
// satisfying the requirements.
a.b
*ptr
$name
a@b.c
// These are keywords.
type
range
The digital versions of this book are available at the following places:
Tapir, the author of Go 101, has been on writing the Go 101 series books
and maintaining the go101.org website since 2016 July.
New contents will be continually added to the book and the website from time to time.
Tapir is also an indie game developer.
You can also support Go 101 by playing
Tapir's games
(made for both Android and iPhone/iPad):
Individual donations via PayPal are also welcome.
Articles in this book:
-
Become Familiar With Go Code