คืนค่าการตั้งค่าทั้งหมด
คุณแน่ใจว่าต้องการคืนค่าการตั้งค่าทั้งหมด ?
ลำดับตอนที่ #8 : Go I - Using Variables
Basics
of Variables
What
are Variables?
Now we will learn about variables. You can think
of a variable as a box where you can put data (values). Also, each variable has a name like variableName, and values can be accessed using the
name. Variables are needed for almost any program, so it's important to
understand how to use them.
Variables
and Data Types
Variables must be defined before you can use them.
To define a variable, you can write var valueName
dataType as shown below. Here, var is
an abbreviation of the word variable. By writing var, you give the computer an instruction to
create a variable.
Data
Types
So far, we have learned the types of values using
words like "strings" and "integers". These are actually
called data types. First,
remember the two data types written below; strings and integers. Strings are a series of
characters, while ints
are integer values.
Using
Variables
The code n = 100 will
assign the value 100 to the variable n. Unlike
the "=" used in math, this does not mean the left and right sides are
equal. In programming, it is a symbol used for inserting a value in a variable
"box". Also, writing println(n) will
print the value of n to the console.
Definition
and Assignment
By writing var n int =
100, you can define a variable and assign it a value simultaneously.
Updating
Variables
Variables can be rewritten (updated) as well. In
order to update variables, new values must be assigned using the = operator. Below, the variable n is printed 2 times and the values 100 and 200
are displayed in the console. This is because n
gets updated with the value 200 before the second println.
Variable
Types
Abbreviating
Data Types
In Go, when variable definitions and value
assignment are done at the same time, like var a
int = 100, the data type can be abbreviated. In the example, "int" is left out so
that only var a = 100 is written. This is
because as 100 is obviously an integer, it can be safely assumed that the data
type assigned to the variable a is an
integer.
Abbreviating
Variable Definitions
Variable definitions can be written without a data
type by using a shortened syntax. As shown below, var
b int = 200 and b := 200 are the
same. Note the colon before the = symbol (:=). This can be very convenient, so give it a
try!
Other
Ways to Use println
There is another useful way to use println. In the example, the variables a and b used
with println are separated by a comma in the parentheses. This allows you to
print two values at the same time. Note that the comma is not printed and that
they are printed on a single line with a space.
Variables
& Errors
Variable
Definitions and Assignment
The same variable cannot be defined multiple
times. On the left, a is defined as a := 200 right after being defined using a := 100 (:= is a variable definition). Here, an error meaning
"there is no new variable on the left of :="
appears in the console.
Where
Variables Can Be Used
Variables can only be used after they are defined.
In the example below, n is used before it is
defined. The console shows an error message meaning "variable n is
undefined".
Unused
Variables
In Go, when variables are defined but are left
unused, errors are displayed. In the example on the left, b is defined but not used. Because of this, an
error is displayed, as shown on the right. Since the unused variable is often
times a mistake, Go prevents bugs from appearing by showing these kinds of
error messages.
Type
Mismatch
Values that do not match variable data types
cannot be assigned. When a variable defined with the int type (n := 100) is
assigned a string type value, the error message meaning "string type value
cannot be assigned to int type" is shown. You can find and fix mistakes by
reading the error messages!
Self-Assignment
Calculations
with Variables
Since variables can be used in the same way as
values, calculations can be made using variables. As in the example below,
addition between variables and integers, variables and variables can be seen.
Self-Assignment
Below, on the left, n
= n + 20 may seem strange when you think of it in a mathematical way.
However, since = means
"assignment" in Go, it should make sense. The code n = n + 20 adds 20 to the original value of n (10) and assigns that value to n (this is called self-assignment). The final
value of n will be 30, as shown in the console.
Shortened
Self-Assignment
Self-assignment means to assign the result of a
calculation using the variable's original value back into the same variable.
This technique is convenient and commonly used in programming. As you can see
below, n = n + 10 can be abbreviated as n += 10, and this is true the symbols -,
*, /, and
%.
When you want to add or subtract 1 from a variable
like n = n + 1 or n
= n - 1, you can write it in an even shorter way. ++ means "add 1 to the variable". Also, -- means "subtract 1 from the variable. This
is very commonly used, so be sure to remember this!
ความคิดเห็น