0% found this document useful (0 votes)
37 views

MODULE 2: GO - Basic Syntax: Web Systems and Technologies

Tokens in Go consist of keywords, identifiers, constants, string literals, and symbols that make up the individual logical pieces of a Go statement. Comments are like helpful text in Go programs and start with /* and end with */. Variables are used to store values and have a specific type like int or float that determines how they are stored and operated on. Variables can be declared using the var keyword and initialized during declaration. Whitespace is important in Go and is used to separate elements of a statement for the compiler.

Uploaded by

Matia Tian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

MODULE 2: GO - Basic Syntax: Web Systems and Technologies

Tokens in Go consist of keywords, identifiers, constants, string literals, and symbols that make up the individual logical pieces of a Go statement. Comments are like helpful text in Go programs and start with /* and end with */. Variables are used to store values and have a specific type like int or float that determines how they are stored and operated on. Variables can be declared using the var keyword and initialized during declaration. Whitespace is important in Go and is used to separate elements of a statement for the compiler.

Uploaded by

Matia Tian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 36

WEB SYSTEMS AND TECHNOLOGIES

MODULE 2: GO – Basic Syntax


Tokens in GO
• A Go program consists of various tokens. A token is either a keyword,
an identifier, a constant, a string literal, or a symbol. For example, the
following Go statement consists of six tokens:

fmt.Println("Hello, World!")
The individual tokens are:
• fmt
• .
• Println
• (
• "Hello, World!“
• )
Line Separator
• In a Go program, the line separator key is a statement terminator.
That is, individual statements don't need a special separator like “;” in
C. The Go compiler internally places “;” as the statement terminator
to indicate the end of one logical entity.
• For example, take a look at the following statements:

fmt.Println("Hello, World!")
fmt.Println("I am in Go Programming World!")
Comments
• Comments are like helping texts in your Go program and they are
ignored by the compiler. They start with /* and terminates with the
characters */ as shown below:

/* my first program in Go */

Note:
You cannot have comments within comments and they do not occur
within a string or character literals.
Identifiers
• A Go identifier is a name used to identify a variable, function, or any
other user-defined item. An identifier starts with a letter A to Z or a to
z or an underscore _ followed by zero or more letters, underscores,
and digits (0 to 9).

identifier = letter { letter | unicode_digit }.


Identifiers
• Go does not allow punctuation characters such as @, $, and % within
identifiers. Go is a case-sensitive programming language.
Thus, Manpower and manpower are two different identifiers in Go.
Here are some examples of acceptable identifiers:

number abc
myName a_123
_temp
Keywords
• The following list shows the reserved words in Go. These reserved
words may not be used as constant or variable or any other identifier
names.
Whitespace in GO
• Whitespace is the term used in Go to describe blanks, tabs, newline
characters, and comments. A line containing only whitespace,
possibly with a comment, is known as a blank line, and a Go compiler
totally ignores it.
• Whitespaces separate one part of a statement from another and
enables the compiler to identify where one element in a statement,
such as int, ends and the next element begins. Therefore, in the
following statement
var age int;
Whitespace in GO
• There must be at least one whitespace character (usually a space)
between int and age for the compiler to be able to distinguish them.
On the other hand, in the following statement:

fruit = apples + oranges; // get the total fruit

• No whitespace characters are necessary between fruit and =, or


between = and apples, although you are free to include some if you
wish for readability purpose.
GO Data Types
• In the Go programming language, data types refer to an extensive
system used for declaring variables or functions of different types.
The type of a variable determines how much space it occupies in
storage and how the bit pattern stored is interpreted.
Boolean types
• They are boolean types and consists of the two predefined constants:
(a) true (b) false
Numeric Types
• They are again arithmetic types and they represents a) integer types
or b) floating point values throughout the program.
String Types
• A string type represents the set of string values. Its value is a
sequence of bytes. Strings are immutable types that is once created,
it is not possible to change the contents of a string. The predeclared
string type is string.
Derived Types
• They include
(a)Pointer types
(b) Array types
(c) Structure types
(d) Union types and
(e) Function types
f) Slice types
g) Interface types
h) Map types
i) Channel Types
Aggregate Types
• Array types and structure types are collectively referred to
as aggregate types. The type of a function specifies the set of all
functions with the same parameter and result types
Integer Types
• The predefined architecture-independent integer types are:
• uint8 - Unsigned 8-bit integers (0 to 255)
• uint16 - Unsigned 16-bit integers (0 to 65535)
• uint32 - Unsigned 32-bit integers (0 to 4294967295)
• uint64 - Unsigned 64-bit integers (0 to 18446744073709551615)
• int8 - Signed 8-bit integers (-128 to 127)
• int16 - Signed 16-bit integers (-32768 to 32767)
• int32 - Signed 32-bit integers (-2147483648 to 2147483647)
• int64 - Signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
Floating Types
• The predefined architecture-independent float types are:
1.float32 - IEEE-754 32-bit floating-point numbers
2.float64 - IEEE-754 64-bit floating-point numbers
3.Complex64 - Complex numbers with float32 real and imaginary parts
4.Complex128 - Complex numbers with float64 real and imaginary
parts

The value of an n-bit integer is n bits and is represented using two's


complement arithmetic operations.
Other Numeric Types
• There is also a set of numeric types with implementation-specific
sizes:
1.byte - same as uint8
2.rune - same as int32
3.uint - 32 or 64 bits
4.int - same size as uint
5.uintptr - an unsigned integer to store the uninterpreted bits of a
pointer value
GO Variables
• A variable is nothing but a name given to a storage area that the
programs can manipulate. Each variable in Go has a specific type,
which determines the size and layout of the variable's memory, the
range of values that can be stored within that memory, and the set of
operations that can be applied to the variable.
• The name of a variable can be composed of letters, digits, and the
underscore character. It must begin with either a letter or an
underscore. Upper and lowercase letters are distinct because Go is
case-sensitive.
GO Variables
• Based on the GO basic types, there will be the following basic variable
types:
• byte - typically a single octet(one byte). This is an byte type.
• int - The most natural size of integer for the machine.
• float32 - A single-precision floating point value.

Go programming language also allows to define various other types of


variables such as Enumeration, Pointer, Array, Structure, and Union.
Variable Definition in GO
• A variable definition tells the compiler where and how much storage
to create for the variable. A variable definition specifies a data type
and contains a list of one or more variables of that type as follows:

var variable_list optional_data_type;

Here, optional_data_type is a valid Go data type including byte, int,


float32, complex64, boolean or any user-defined object, etc.
variable_list
• variable_list may consist of one or more identifier names separated
by commas. Some valid declarations are shown here:
var i, j, k int;
var c, ch byte;
var f, salary float32;
d = 42;
The statement “var i, j, k;” declares and defines the variables i, j and k;
which instructs the compiler to create variables named i, j, and k of
type int.
GO Variables
• Variables can be initialized (assigned an initial value) in their
declaration. The type of variable is automatically judged by the
compiler based on the value passed to it. The initializer consists of an
equal sign followed by a constant expression as follows:
variable_name = value;
Example: d = 3, f = 5; // declaration of d and f. Here d and f are int

For definition without an initializer: variables with static storage duration


are implicitly initialized with nil (all bytes have the value 0); the initial
value of all other variables is zero value of their data type.
Static Type Declaration in GO
• A static type variable declaration provides assurance to the compiler
that there is one variable available with the given type and name so
that the compiler can proceed for further compilation without
requiring the complete detail of the variable.
• A variable declaration has its meaning at the time of compilation only,
the compiler needs the actual variable declaration at the time of
linking of the program.
Example
package main
import "fmt"
func main() {
var x float64
x = 20.0
fmt.Println(x)
fmt.Printf("x is of type %T\n", x)
}
Output
20
x is of type float64
Dynamic Type Declaration / Type Inference
A dynamic type variable declaration requires the compiler to interpret
the type of the variable based on the value passed to it. The compiler
does not require a variable to have type statically as a necessary
requirement.
Example
• the following example, where the variables have been declared
without any type. Notice, in case of type inference, we initialized the
variable y with := operator, whereas x is initialized using = operator.
Output
20
42
x is of type float
64 y is of type int
Mixed Variable Declaration in GO
• Variables of different types can be declared in one go using type
inference.
Example
Output
3
4
foo
a is of type int
b is of type int
c is of type string
The Ivalues and the rvalues in GO
• There are two kinds of expressions in Go:
1.lvalue − Expressions that refer to a memory location is called "lvalue"
expression. An lvalue may appear as either the left-hand or right-hand
side of an assignment.
2.rvalue − The term rvalue refers to a data value that is stored at some
address in memory. An rvalue is an expression that cannot have a
value assigned to it which means an rvalue may appear on the right-
but not left-hand side of an assignment.
The Ivalues and the rvalues in GO
• Variables are lvalues and so may appear on the left-hand side of an
assignment. Numeric literals are rvalues and so may not be assigned
and can not appear on the left-hand side.

Valid: x = 20.0
Invalid: 10 = 20
Reference
• Prime Packs. (n.d.). Retrieved from
https://www.tutorialspoint.com/index.htm

You might also like