//
, and each block comment is enclosed in a pair of /*
and */
.
package main // specify the source file's package
import "math/rand" // import a standard package
const MaxRnd = 16 // a named constant declaration
// A function declaration
/*
StatRandomNumbers produces a certain number of
non-negative random integers which are less than
MaxRnd, then counts and returns the numbers of
small and large ones among the produced randoms.
n specifies how many randoms to be produced.
*/
func StatRandomNumbers(n int) (int, int) {
// Declare two variables (both as 0).
var a, b int
// A for-loop control flow.
for i := 0; i < n; i++ {
// An if-else control flow.
if rand.Intn(MaxRnd) < MaxRnd/2 {
a = a + 1
} else {
b++ // same as: b = b + 1
}
}
return a, b // this function return two results
}
// "main" function is the entry function of a program.
func main() {
var num = 100
// Call the declared StatRandomNumbers function.
x, y := StatRandomNumbers(num)
// Call two built-in functions (print and println).
print("Result: ", x, " + ", y, " = ", num, "? ")
println(x+y == num)
}
basic-code-element-demo.go
and run this program by:
$ go run basic-code-element-demo.go Result: 46 + 54 = 100? true
package
, import
, const
, func
, var
, for
, if
, else
, and return
are all keywords. Most other words in the program are identifiers. Please read keywords and identifiers for more information about keywords and identifiers.
int
words at line 15 and line 17 denote the built-in int
type, one of many kinds of integer types in Go. The 16
at line 5, 0
at line 19, 1
at line 22 and 100
at line 32 are some integer literals. The "Result: "
at line 36 is a string literal. Please read basic types and their value literals for more information about above built-in basic types and their value literals. Some other types (composite types) will be introduced later in other articles.
MaxRnd
. Line 17 and line 32 declare three variables, with the standard variable declaration form. Variables i
at line 19, x
and y
at line 34 are declared with the short variable declaration form. We have specified the type for variables a
and b
as int
. Go compiler will deduce that the types of i
, num
, x
and y
are all int
, because they are initialized with integer literals. Please read constants and variables for more information about untyped values, type deduction, value assignments, and how to declare variables and named constants.
<
at line 19 and 21, the equal-to operator ==
at line 37, and the addition operator +
at line 22 and line 37. Yes, +
at line 36 is not an operator, it is one character in a string literal. The values involved in an operator operation are called operands. Please read common operators for more information. More operators will be introduced in other articles later.
print
and println
, are called. A custom function StatRandomNumbers
is declared from line 15 to line 28, and is called at line 34. Line 21 also calls a function, Intn
, which is a function declared in the math/rand
standard package. A function call is a function operation. The input values used in a function call are called arguments. Please read function declarations and calls for more information.
print
and println
functions are not recommended to be used in formal Go programming. The corresponding functions in the fmt
standard packages should be used instead in formal Go projects. In Go 101, the two functions are only used in the several starting articles.)
main
entry function must be declared in a package which is also called main
. Line 3 imports a package, the math/rand
standard code package. Its import name is rand
. The function Intn
declared in this standard package is called at line 21. Please read code packages and package imports for more information about how to organize code packages and import packages.
StatRandomNumbers
function body, two control flows are used. One is a for
loop control flow, which nests the other one, an if-else
conditional control flow. Please read basic control flows for more information about all kinds of basic control flows. Some other special control flows will be introduced in other articles later.
StatRandomNumbers
function, other comments are for demonstration purpose only. We should try to make code self-explanatory and only use necessary comments in formal projects.
{
and }
) to form an explicit code block. However, in Go programming, coding style can't be arbitrary. For example, many of the starting curly braces ({
) can't be put on the next line. If we modify the StatRandomNumbers
function declaration in the above program as the following, the program will fail to compile.
func StatRandomNumbers(n int) (int, int)
{ // syntax error
var a, b int
for i := 0; i < n; i++
{ // syntax error
if rand.Intn(MaxRnd) < MaxRnd/2
{ // syntax error
a = a + 1
} else {
b++
}
}
return a, b
}
The Go 101 project is hosted on Github. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.
If you would like to learn some Go details and facts every serveral days, please follow Go 101's official Twitter account @zigo_101.
reflect
standard package.sync
standard package.sync/atomic
standard package.