GO Programming, For Beginners, Quick Start Guide by Yao, Ray 2015
GO Programming, For Beginners, Quick Start Guide by Yao, Ray 2015
me/ThDrksdHckr
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Go
Programming
For Beginners
Quick Start Guide
Ray Yao
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Copyright © 2015 by Ray Yao All Rights Reserved Neither part of this
book nor whole of this book may be reproduced or transmitted in any
form or by any means electronic, photographic or mechanical, including
photocopying, recording, or by any information storage or retrieval
system, without prior written permission from the author. All rights
reserved!
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Ray Yao
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Preface
“Go Programming” covers all essential Go language
knowledge. You can learn complete primary skills of Go
programming fast and easily.
The book includes more than 60 practical examples for
beginners and includes tests & answers for the college
exam, the engineer certification exam, and the job interview
exam.
Note: This book is only for Go beginners, it is not
suitable for experienced Go programmers.
Table of Content
Hour 1
What is Go?
Install Go
Environment Variables
First Program of Go
Comment
Identifier
Summary
Hour 2
Line Separator
About “{”
Keywords
Data Type
Variables
Multiply Variables’ Declaration
:= assignment operator
Summary
Hour 3
Constants
iota
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Operators Precedence
Summary
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Hour 4
If Statement
if…else statement
Switch Statement
Select Statement
For Loop
Summary
Hour 5
Break
Continue
Goto
Function
Working Scope
Summary
Hour 6
Array
Pointers
Nil Pointer
Summary
Hour 7
Structures
Slice
append() & copy()
Summary
Hour 8
GOROUTINE
Range
Map
Type Conversion
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Recursion
Summary
Appendix 1
Concurrency
Appendix 2
Tests
Answers
Source Code Download
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Hour 1
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
What is Go?
Go is an open source programming language released by
Google in 2009.
Go is a new language, a concurrent and fast compiled
language with garbage collection. It has a great
characteristic: it can compile a large Go program in a few
seconds on a computer. Go provides a model for software
construction that makes dependency analysis easier. Go is a
statically typed language, and its type definition has no
hierarchy, so users don't have to spend time defining
relationships between types, which make users feel that it is
lighter than a typical object-oriented language. Go can
provide a way to construct system software on a multicore
machine.
The Go language programming is optimized for
multiprocessor system applications that can be faster than
C or C++ code and more securely support parallel
processes.
The feature of the Go language:
1. Simple, fast and safe
2. Parallel, interesting, open source
3. Memory management, array security, fast
compilation.
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
commands:
Comment
//
/*
…….
*/
“// ” is the symbol of single-line comment.
“/* ” and “*/ ” are the symbols of multi-line comment.
Comments will not be compiled. The Go compiler will ignore
all comments.
Example 1.2
package main // package declaration import "fmt"
func main() { // define a main function fmt.
Println("Hello, World! ")
}
/*
The output of the above code is: Hello, World!
*/
Please use “go run ” command to run this program, and
check the result.
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Hour 2
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Line Separator In a Go
language, each statement does
not need a semicolon to end the
line.
For example:
There are two lines in the above code, one line represents
the end of a statement, each statement don’t need a
semicolon to end the line.
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
About “{”
“{“ cannot be placed on a single line, so the following code
produces an error at run time.
For example:
Keywords
There are 25 keywords in Go language.
break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return va
Data Type
In Go language, when we define a variable or function, we
need to use a data type.
The use of data types is conducive to Go programming.
Chiefly, there are four kinds of data type in Go language,
they are very commonly used in Go coding.
1. Boolean types
2. Numeric types
3. String types
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
4. Derived types
variable_name : = value
For example: c : = 100
Example 2.1
package main func main(){
var a = "C# in 8 Hours"
var b string = "is"
c: ="a good book! "
var e bool = true println(a, b, c, e) }
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
:= assignment operator
variable := value
Example 2.2
package main func main(){
var a, b, c int a, b, c = 100, 200, 300
var d, e, f = 400, 500, 600
s1, s2, s3 := "R in 8 Hours", "is", "good book. "
println(a, b, c) println(d, e, f) println(s1, s2, s3) }
Summary In a Go language,
each statement does not need a
semicolon to end the line.
“{“ cannot be placed on a single line, so the following code
produces an error at run time.
Keywords cannot work as a name of variable, constant and
function, etc.
Variable names of Go language consist of letters, numbers
and underscores, where the first character cannot be a
number.
There are three ways to declare the multi-variables: 1.
var vname1, vname2, vname3 type vname1, vname2,
vname3 = v1, v2, v3
2.
var vname1, vname2, vname3 = v1, v2, v3
3.
vname1, vname2, vname3 : = v1, v2, v3
:= assignment operator is used to assign a value to a
variable.
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Hour 3
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Constants
A constant is an identifier whose value is not changed while
the program is running.
The syntax to define a constant is:
const contant_name [type] = value
Or
Example 3.1
package main func main() {
const x, y, z int = 10, 20, 30 // define three constants const
s = "jQuery in 8 Hours! "
const b bool = true
println(x, y, z) println(s) println(b) }
Output:
10 20 30
jQuery in 8 Hours!
true Explanation: “const” is used to define a constant.
Example 3.2
package main // Define constants as enumerations func
main() {
const ( // enumeration color1 = "Blue"
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
color2 = "Pink"
color3 = "Gray"
)
println(color1, color2, color3) }
Output: Blue Pink Gray Explanation: The constants
color1, color2, color3 is defined in an enum mode.
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Output: 0
1
2
Explanation:
The first iota value is 0, the second iota value is 1, the third
iota value is 2.
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Arithmetic Operators
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Get Remainder
++ Increase 1
-- Decrease 1
For example:
Given: a = 100 b = 2
Calculation:
a + b returns 102
a – b returns 98
a * b returns 200
a / b returns 50
a % b returns 0
a ++ returns 101
a - - returns 99
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Relational Operators
Operators Running
> greater than
< less than
>= greater than or equal
<= less than or equal
== equal
!= not equal
Logical Operators
Operators Equivalent
&& and
|| or
! not
Explanation:
true &&
true && false; false &&false;
true; returns
returns false; returns false;
true;
true II true; true II false; false II false;
returns true; returns true; return false;
! false; ! true;
returns true; returns false;
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Assignment Operators
For exampl e :
Given: x=200; y=100; Explanation: x+=y; // x=x+y; outputs
300
x/ =y; // x=x / y; outputs 2
x- =y; // x=x - y; outputs 100
x%=y; // x=x%y; outputs 0
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Operators Precedence
The following table lists the operator precedence:
(From top to bottom means priority from high to low).
Priority Operators
7 !
6 * / %
5 + -
4 == ! =
3 <= <=
2 &&
1 ||
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Summary A constant is an
identifier whose value is not
changed while the program is
running.
The syntax to define a constant is: const contant_name
[type] = value const contant_name = value “iota” is a
special constants. The value of the first iota is equal to 0,
and its value is automatically incremented by 1 whenever
iota is used in a new row.
There are following operators: arithmetic operators,
relational operators, logical operators, assignment
operators.
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Hour 4
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
If Statement
Example 4.1
package main import "fmt"
func main() {
var num int = 100
if num > 50 {
fmt. Printf("num is greater than 50" ) }
}
Output: num is greater than 50
Explanation: Because the value of num is greater
than 50, the code inside the { } has been executed.
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
if…else statement
if test_expression{
// if true do this } else {
// if false do this }
“if... else statement” runs some code if a condition is true
and runs another code if the condition is false.
Example 4.2
package main func main() {
var num int = 100; if num > 200 {
println("num is greater than 200" ); } else {
println("num is less than 200" ); }
}
Switch Statement
Example4.3
package main func main() {
var number int=20
switch ( number ) { // number value compares each case
case 10 : println ( "Running case 10" ) case 20 : println (
"Running case 20" ) case 30 : println ( "Running case 30" )
default : println ( "Running default code" ) }
}
(3)
select {
case receive or send data : statement(s); case receive
or send data : statement(s); default : // optional
statement(s); }
If more than one case can be run, Select will randomly and
fairly pick one to execute and the others will not.
If no case can be run, Select will execute default clause.
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Example 4.4
package main import "fmt"
func main() {
c1:=make (chan int,1024) // create a channel c2:=make
(chan int,1024)
c3:=make (chan int,1024)
c1 <- 100 // send data to a channel c2 <- 200
c3 <- 300
select { // randomly execute one case .
case <- c1: // receive data from a channel fmt.
Println("Receive data from c1") case <- c2:
fmt. Println("Receive data from c2") case <- c2:
fmt. Println(Rreceive data from c3") default: fmt.
Println("Not receive data yet") }
}
012345
Explanation:
(2)
for test_expression {
}
12345
Explanation:
Hour 5
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Break
break
Example 5.1
package main func main() {
var num int = 20
for num < 30 {
print(num," "); num++;
if num > 25 {
break; // terminate the for loop }
}
}
Output:
20 21 22 23 24 25
Explanation:
Continue
continue
Example 5.2
package main func main() {
var num int = 20
for num < 30 {
if num == 25 {
num = num + 1; continue; // skip the current for
loop }
print(num, " ") num++;
}
}
Output:
20 21 22 23 24 26 27 28 29
Goto
Example 5.3
package main func main() {
var num int = 10
if num > 10{
goto code01
}
if num == 10{
goto code02
}
code01: print("num > 10")
code02: print("num == 10")
}
Output:
num == 10
Example 5.4
package main func main() {
var a int = 10
var b int = 20
var result int result = add(a, b) // call the function print(
"The sum is ", result ) }
func add(num1, num2 int) int { // define a function
return num1 + num2 // pass the result to the caller }
and num2.
func add(num1, num2 int) int defines a function add(…),
num1 and num2 receive the value from a and b.
return num1 + num2 passes the result value to the caller
add(…).
Example 5.5
package main import "fmt"
func swap (a, b string) (string, string) { // define a function
return b, a // returns two values to the caller .
}
func main() {
x, y : = swap("in 8 Hours", "JavaScript") //call the
function fmt. Println(x, y)
}
Example 5.6
package main func main() {
var a, b, c int // defines local variables a = 100
b = 200
c = a + b print(a, b, c) }
Output:
Example 5.7
package main var v int = 100 // define a global variable
func main(){
print ("The value of the global variable is ", v) }
Output: The value of the global variable is 100.
Explanation: var v int = 100 defines a global variable.
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Hour 6
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Output:
100 101 102
(2)
Example 6.2
package main func main(){
var arr [ ]int = make([ ]int, 3) // define an array
arr[0]=100 // assign a value to the array element
arr[1]=101
arr[2]=102
print(arr[0], " ", arr[1], " ", arr[2]) }
Output:
100 101 102
(3)
Output:
var arr = [3] int {100,101, 102} defines an array with three
values 100, 101, and 102.
(4)
Output:
100 101 102
Example 6.5
package main
import "fmt"
func main() {
var num int = 10
fmt. Printf("The address of the variable: %x\n", &num )
}
Output:
The address of the variable: C000044058
Explanation:
&num returns the address of the variable num.
(2)
var variable_name * type // declare a pointer
*variable_name // get the variable value in the address
For example:
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Example 6.6
package main
import "fmt"
func main() {
var num int= 100
var pt *int // declare a pointer pt pt = &num // &num
get address and assign to pt fmt. Printf("The address of
num: %x\n", &num ) fmt. Printf("The address in pt: %x\n", pt
)
fmt. Printf("The value of the *pt: %d\n", *pt ) }
Output:
The address of num: C000044058
The address in pt: C000044058
The value of the *pt: 100
Explanation:
&num gets the memory address from num.
pt = &num assigns the memory address to pt *pt returns
the pt value in the memory address.
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Example 6.7
package main
import "fmt"
func main() {
var a string="Hello"
var b *string
var c string
b=&a //&a gets the memory address c=*b //*b gets the
value in memory address fmt. Println(a)
fmt. Println(b)
fmt. Println(c)
}
Output:
Hello
0xc0000381c8
Hello
Explanation:
&varilabe returns a memory address.
*variable returns a value in the memory address.
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Output:
The address stored in npt: 0
Summary An array is a
collection of variables of the
same type and name.
Array element: each variable in an array.
Array index: also called as key, the numbers on each
element.
Note: the array index or array key starts from zero.
A pointer represents the value of a memory address.
&variable // get the memory address of a variable var
variable_name * type // declare a pointer *variable_name //
get the variable value in the address When a pointer is
defined and not assigned to any variable address, its value
is nil.
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Hour 7
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Structures A structure is a
collection of data with the same
or different types of data.
(1) Define Structure Type.
Example 7.1
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
package main
import "fmt"
type Flower struct { // define a structure type color string
// structure member size string
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
number int
}
func main() {
fmt. Println(Flower{color:"Red", size:"Large",
number:10 })
}
Output:
Red Large 10
Explanation:
“type Flower struct” defines a structure type (3) Declare
Structure Variables
var variable structure_name
Example 7.2
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
package main
type Flower struct { // define a structure type color string
// structure member size string
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
number int
}
func main() {
var Rose Flower // declare a structure variable Rose
Rose.color = "Red" // assign value to structure member
Rose.size = "Large"
Rose.number = 10
Example 7.3
package main import "fmt"
func main(){
s := [ ]int{1,2,3,4,5} // create a slice fmt. Println(s)
}
Output:
12345
Explanation:
Example 7.5
package main import "fmt"
func main() {
var s = [9]int{0, 1, 2, 3, 4, 5, 6, 7, 8}
mySlice:= s[2:6] // get the value from s[2] to s[5]
fmt. Println(mySlice)
}
Output:
2345
Explanation: “s[2: 6]” gets the value from s[2] to s[5]
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Example 7.6
package main import "fmt"
func main() {
var s = [9]int{0, 1, 2, 3, 4, 5, 6, 7, 8}
s[3] = 10 // assign a new value to s[3]
s[6] = 100 // assign a new value to s[6]
fmt. Println(s)
}
Output:
[0 1 2 10 4 5 100 7 8]
Explanation: s[3] = 10 // assign a new value to s[3]
s[6] = 100 // assign a new value to s[6]
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Example 7.7
package main import "fmt"
func main() {
mySlice : = make([]int, 0, 0)
showSlice(mySlice) mySlice = append(mySlice, 10,20,30)
// append showSlice(mySlice) newSlice : = make([]int,
len(mySlice), (cap(mySlice))*2) copy(newSlice,mySlice) //
copy showSlice(newSlice) }
Summary A structure is a
collection of data with the same
or different types of data.
(1) Define Structure Type.
type structure_name struct {
member1 type; member2 type; ...
member3 type; }
(2) Access Structure Members structure_name{member1:
value1, member2: value2, …}
(3) Declare Structure Variables var variable structure_name
(4) Assign Values to Structure Members variable . member
= value
Slice is a special array, but its length is changeable.
append(slice_name, values) // append values to a Slice
copy(new_slice, old_slice) // copy old Slice to new Slice
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Hour 8
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
GOROUTINE
Example 8.1
package main
import "fmt"
func fn(s string) { // define a function fn for i := 0; i < 5; i++
{
fmt. Println(s)
}
}
func main() {
fn("Thread B") // call the function fn go func(msg string) { //
define a goroutine function fmt. Println(msg)
} ("Press Enter to Stop! ") // run the goroutine function fmt.
Scanln() // accept user input from keyboard fmt.
Println("Complete! ")
}
Output:
Thread B
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Thread B
Thread B
Thread B
Thread B
Press Enter to Stop!
Explanation:
go func(msg string) {…} defines a goroutine function.
("Press Enter to Stop! ") runs the goroutine function, and
pass the parameter to the gorountine function.
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Example 8.2
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
package main
import "fmt"
func main(){
for key, value : = range[ ]int{10, 11, 12, 13} {
fmt. Printf("key:%d value:%d\n", key, value) }
}
Output:
key: 0 value: 10
key: 1 value: 11
key: 2 value: 12
key: 3 value: 13
Explanation:
range[ ]int{10, 11, 12, 13} iterates over an array or slice,
and returns the keys and values.
Example 8.3
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
package main
import "fmt"
func main(){
var str = "hello"
for key, value : = range str {
fmt. Printf("key:%d value:0x%x\n", key, value) }
}
Output:
key : 0 value : 0x68
key : 1 value : 0x65
key : 2 value : 0x6c key : 3 value : 0x6c key : 4 value : 0x6f
Explanation:
“range str { }” iterate over a string, returns its keys and
values.
Example 8.4
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
package main
import "fmt"
func main(){
c : = make(chan int)
go func() { // define a goroutine function c <- 1 // send
datas to channels c <- 2
c <- 3
close(c) // close channels } ( ) // call the goroutine
function for v := range c { // iterate over the channels fmt.
Println("Send data to channel: ",v)
}
}
Output:
Send data to channel: 1
Send data to channel: 2
Send data to channel: 3
Explanation:
“range c” iterates over the channels.
go func(){ defines a goroutine function.
} ( ) calls the goroutine function.
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
(2)
m2 : = make(map[key_type] value_type)
Example 8.5
package main import "fmt"
func main() {
m1 := map[int]int{} // define a map m1
m2 := make(map[int]int) // define a map m2
m1[1] = 10
m2[2] = 20
for key, value : = range m1 {
fmt. Println("Key: ", key, "Value: ", value)
}
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Example 8.6
package main import "fmt"
func main() {
var num1 int = 100
var num2 int = 3
var result float32
result = float32(num1)/float32(num2) // conversion fmt.
Printf("The result is %f\n", result) }
Output: The result is 33. 333332
Explanation: float32(num1)/float32(num2) converts
the data type from int into float32.
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Example 8.7
package main import "fmt"
func myRecur(n int) int {
if n == 0 {
return 1
}
return n * myRecur(n-1) // call itself }
func main() {
fmt. Print("Factorial of 10 is ") fmt. Print(myRecur(10))
}
Appendix 1
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Concurrency Go language
supports concurrency, we can
use “go” keyword to call a
function, start a new runtime
thread goroutine.
The syntax to call a function with go keyword is as follows:
go function_name (arguments)
Example:
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
package main
import (
"fmt"
"time"
)
func fn(s string) { // define a function fn for i : = 0; i < 5;
i++ {
time. Sleep(200 * time. Millisecond) // delay running fmt.
Println(s)
}
}
func main() {
go fn("Thread A") // call the function fn fn("Thread B") //
call the function fn go func(msg string) { // define a
goroutine function fmt. Println(msg)
}("Press Enter to Stop! ") // run the goroutine function fmt.
Scanln() // accept user input from keyboard fmt.
Println("Complete! ")
}
Output:
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Thread B
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Thread A
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Thread B
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Thread A
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Thread B
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Thread A
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Thread B
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Thread A
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Thread B
Press Enter to Stop!
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Thread A
Complete!
Explanation:
go fn("Thread A") calls the function fn with “go” keyword,
start a new thread goroutine.
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Appendix 2
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
(02)
fill in a int // declare a variable a = 100
(03)
package main func main() {
const (
x = fill in // a special constant y = fill in z = fill in )
print(x, y, z) }
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Output: 0 1 2
(04)
package main func main() {
var number int=20
fill in ( number ) { // let number compares each case case
10 : println ( "Running case 10" ) case 20 : println (
"Running case 20" ) case 30 : println ( "Running case 30" )
default : println ( "Running default code" ) }
}
A. If B. While C. Do D. Switch
(05)
package main func main() {
var num int = 10
if num > 10{
fill in code01 // run the specified code next }
if num == 10{
fill in code02 // run the specified code next }
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
A. goto B. go C. to D. return
(06)
package main func main(){
var arr [ ]int = fill in ([ ]int, 3) // define an array
arr[0]=100 // assign a value to the array element
arr[1]=101
arr[2]=102
print(arr[0], " ", arr[1], " ", arr[2]) }
(07)
package main import "fmt"
type Flower fill in { // define a structure type color string //
structure member size string number int }
func main() {
fmt. Println(Flower{color:"Red", size:"Large",
number:10 })
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
(08)
package main import "fmt"
func fn(s string) { // define a function fn for i := 0; i < 5; i++
{
fmt. Println(s)
}
}
func main() {
fn("Thread B") // call the function fn fill in func(msg string)
{ // define a goroutine function fmt. Println(msg)
} ("Press Enter to Stop! ") // run the goroutine function fmt.
Scanln() // accept user input from keyboard fmt.
Println("Complete! ")
}
A. goto B. go C. to D. goroutine
(09)
package main func main(){
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
(10)
Given: a = 100 b = 2
Calculation: a + b returns 102
a – b returns 98
a * b returns 200
a / b returns 50
a % b returns fill in A. 0 B. 1. C. 2 D. 3
(11)
package main import "fmt"
func main() {
c1:=make (chan int,1024) // create a channel c2:=make
(chan int,1024)
c3:=make (chan int,1024)
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
(12)
package main import "fmt"
fill in swap (a, b string) (string, string) { // define a function
return b, a // returns two values to the caller .
}
func main() {
x, y : = swap("in 8 Hours", "JavaScript") //call the
function fmt. Println(x, y)
}
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
(13)
package main import "fmt"
func main() {
var num int= 100
var pt *int // declare a pointer pt pt = fill in // &num get
address and assign to pt fmt. Printf("The address of num:
%x\n", &num ) fmt. Printf("The address in pt: %x\n", pt )
fmt. Printf("The value of the *pt: %d\n", *pt ) }
(14)
package main import "fmt"
func main(){
s := fill in int{1,2,3,4,5} // create a slice fmt. Println(s)
}
(15)
package main import "fmt"
func main(){
c : = make(chan int)
go func() { // define a goroutine function c <- 1 // send
datas to channels c <- 2
c <- 3
close(c) // close channels } ( ) // call the goroutine
function for v := fill in c { // iterate over the channels fmt.
Println("Send data to channel: ",v)
}
}
(16)
package main func main(){
var a = "C# in 8 Hours"
var b string = "is"
c fill in "a good book! " // assign value to c var e bool =
true println(a, b, c, e) }
A. <- B. : = C. = D. <=
(17)
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
true &&
true && false; false &&false;
true; returns
returns false; returns false;
true;
true II false;
true II true; false II false;
returns fill in
returns true; return fill in ;
;
! false; ! true;
returns true; returns false;
A. true true
B. false false
C. true false
D. false true
(18)
package main func main() {
var a int =0
var b int = 5
fill in a < b { // loop statement a++
print(a)
}
}
Output:
12345
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
A. while B. if C. do D. for
(19)
package main import "fmt"
func swap (a, b string) (string, string) { // define a function
fill in b, a // pass two values to the caller .
}
func main() {
x, y : = swap("in 8 Hours", "JavaScript") //call the
function fmt. Println(x, y)
}
(20)
package main import "fmt"
func main() {
var npt *int fmt. Printf( npt )
}
// What is the output?
A. 0 B. 1 C. 2 D. 3
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
(21)
package main
import "fmt"
func main() {
var s = [9]int{0, 1, 2, 3, 4, 5, 6, 7, 8}
mySlice:= s[ fill in ] // get four values fmt.
Println(mySlice)
}
Output:
2345
A. 2: 6 B. 1: 5 C. 3: 7 D. 3: 6
(22)
package main import "fmt"
func main() {
m1 := fill in [int]int{} // define a map m1
m2 := fill in (map[int]int) // define a map m2
m1[1] = 10
m2[2] = 20
for key, value : = range m1 {
fmt. Println("Key: ", key, "Value: ", value)
}
for key, value : = range m2 {
fmt. Println("Key: ", key, "Value: ", value)
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
}
}
A. make make
B. make map
C. map make
D. map map
(23)
package main import "fmt"
func myRecur(n int) int {
if n == 0 {
return 1
}
return n * fill in // recursion }
func main() {
fmt. Print("Factorial of 10 is ")
fmt. Print(myRecur(10))
}
A. myRecur(0)
B. myRecur(n-1)
C. recursion(0)
D. recursion(n-1)
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
(24)
package main import (
"fmt"
"time"
)
func fn(s string) { // define a function fn for i : = 0; i < 5;
i++ {
time. Sleep(200 * time. Millisecond) // delay running fmt.
Println(s)
}
}
func main() {
fill in fn("Thread A") // call the function fn fn("Thread B") //
call the function fn }
A. goroutine B. goto C. to D. go
https://t.me/bookzillaaa - https://t.me/ThDrksdHckr
Answers
Note:
This book is only for beginners, it is not suitable for
experienced programmers .