I'm going to write the understanding of Go from the basics of programming!
package main
func main(){
println("Hello, Go")
}
Error occurs if not enclosed in "" (double quotes)
package main
func main(){
println("Hello, Go")
}
//console
Hello, Go
println is one of the "instructions" provided by Go.
Command "Print what's in println () to the console" → The computer outputs the character string to the console
package main
func main(){
println("Hello, Go")
}
The Go code file is composed of the package definition part and the function definition part.
Here, if you write the instruction in the {} of the main function of the file written as "package main" It is executed in order
package main
func main(){
println(1)
println(1 + 1)
println(1 - 2)
println("1 - 2")
}
//console
1
2
-1
1 - 2
Numbers are not enclosed in double quotes, unlike strings
package main
func main(){
println(2 * 5) //multiplication
println(6 / 2) //division
println(9 % 2) //Put out the remainder
}
//console
10
3
1
Symbols used for such operations are called "operators" The type of operator depends on the programming language
package main
func main(){
println("Hi" + "Go")
println("1" + "2")
}
//console
Hi,Go
12
Use "+" on a string to make it a single string
Box to store data (value) The box (variable) has a name (variable name), and you can retrieve the value from the variable by using the name.
var number int
//number(Variable name)、int(Data type)
var number string
//number(Variable name)、string(Data type)
To use a variable, first define the variable "Var variable name data type"
Instruct the computer to create a variable
"Hello, Go" //String(string type)
3 //integer(int type)
Strings and numbers are called "data types"
package main
func main(){
var n int //Definition of variable n
n = 100 //Put 100 in the variable n(substitute)order
println(n) //Output 100 using n
}
//console
100
"=" Is a symbol for putting a value in a box called a variable
package main
func main(){
var n int = 100
println(n)
}
//console
100
If you write "var n int = 100", you can define and assign variables at the same time.
package main
func main() {
var message string = "Hello,world" //Define and assign the variable message
println(message)
}
package main
func main(){
var n int = 100
println(n)
n = 200
println(n)
}
//console
100
200
Variables can be overwritten as many times as you like
package main
func main(){
var n = 100
println(n)
}
//console
100
When variable definition and value assignment are performed at the same time, such as "var n int = 100" Data type specification can be omitted
Because it is judged from the assigned value that Go is an int type
package main
func main(){
n := 100
println(n)
}
//console
100
Same meaning as "var n int = 100" Note that it is ": ="
package main
func main(){
a := 100
b := 200
println(a,b)
}
//console
100 200
If you write variables a and b side by side in () of println separated by commas Two values can be output at the same time
Commas are not output, separated by spaces and displayed on one line
Recommended Posts