Go Language-Basic ❶

Basic ❶

I'm going to write the understanding of Go from the basics of programming!

What is a character string?

package main
func main(){
  println("Hello, Go")
}

Error occurs if not enclosed in "" (double quotes)

Output instructions

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

Program structure

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

Use numbers

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

Perform the calculation

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

String concatenation

package main
func main(){
  println("Hi" + "Go")
  println("1" + "2")
}

//console
Hi,Go
12

Use "+" on a string to make it a single string

What is a variable?

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.

Variables and data types

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

Data type

"Hello, Go" //String(string type)
3 //integer(int type)

Strings and numbers are called "data types"

Use variables

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

Definition and assignment

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)
}

Update the value of a variable

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

Data type abbreviation

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

Omission of variable definition

package main
func main(){
  n := 100
  println(n)
}

//console
100

Same meaning as "var n int = 100" Note that it is ": ="

Convenient way to write println

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

Go language-Basic ❷ is here

Recommended Posts

Go Language-Basic ❶
Go Language-Basic ❷
Go language-fmt.Scan
Go language-address
Go Language-Functions
Go language-pointer
GO Chokisomemo 1
Go language-iteration
Python with Go
Go language-conditional branching
About Go functions
Go language-standard package
Go class basics
Go language-rand package
About Go Interface
use go module