go language learning miscellaneous notes 1

I was studying Go language a while ago, and when I have a memorandum, I will write a memo at that time.

package

go is composed of units called packages, and imports and exports are performed in units of functions and variables in it. Whether it can be done is determined by the case of the letters.

 package main
 
 import (
 	"fmt"
 	"math"
 )
 
 func main() {
 	fmt.Println(math.Pi) //OK 
 	fmt.Println(math.pi)  // error
 }

Uppercase letters are not exported If it is lowercase, it will be exported.

Variable declaration

Variables are declared in the form of var . In addition, it is applied like a split assignment.

You can use the walrus operator (: =) to make type inference and declare type names omitted. In that case, the declaration will be : =

The constant is const = . In the case of a constant, type inference works without permission.

 var i, j int = 1, 2
 test := "string" //Variable initialization with type inference
 const Pi = 3.14 //Constants, in this case type inference cannot be used to initialize constants

Basic type

go has a function called zero value, and if a value is not assigned with a specific type, the value is automatically assigned. Therefore, you can prevent the case where you forget to enter the value and it becomes null.

 bool //zero value is false
 
 string //zero value is""
 
 int  int8  int16  int32  int64 //zero value is 0
 uint uint8 uint16 uint32 uint64 uintptr
 
 byte //Another name for uint8
 
 rune //Another name for int32
      //Represents a Unicode code point
 
 float32 float64
 
 complex64 complex128

For some reason, there is a complex number in the basic type.

Type conversion

Type conversion is possible with ()

 var i int = 42
 var f float64 = float64(i)
 var u uint = uint(f)

Recommended Posts

go language learning miscellaneous notes 1
Go language learning record
Python learning notes
python learning notes
Go language environment construction
Device mapper learning notes
First Python miscellaneous notes
Go language cheat sheet
Features of Go language
O'Reilly python3 Primer Learning Notes
Basics of Machine Learning (Notes)
Python data analysis learning notes
[Golang] About Go language channel
Hello World in GO language
Try implementing Yubaba in Go language
Use optinal type-like in Go language
[Language processing 100 knocks 2020] Chapter 6: Machine learning
100 Language Processing Knock 2020 Chapter 6: Machine Learning
Let's touch on the Go language
Post to slack in Go language
Go Language Spreadsheet Library: Excelize 2.3.2 Released
2021 Go Language Framework Star Count Comparison
A Tour of Go Learning Summary
100 language processing knock-73 (using scikit-learn): learning
Write tests in GO language + gin
Do something object-oriented in GO language