[Go] Basic grammar ① Definition

Now that I've finished looking at the "definition" section of the udemy course, I'll review it.

I learned while outputting the basic operation on the console.

Premise

--Learn Go by watching Introduction to Go taught by active Silicon Valley engineers + Development of Bitcoin Systre Fintech app by application on udemy During ~

--Development environment is VS Code + plugin (Go version is 1.15) --OS is Windows

Library

-You can read the explanation at "document" on the go site or at the command prompt. -Import and use outside the function.

variable

-If the declared variable is not used, a compile error will occur. ・ It is also possible to declare all at once ↓

	var (
		a int
		b float64
		c string
		d bool
	)

-In a function, it can be declared without specifying the type. (The type is automatically determined.) -Variable types can be checked using functions in the fmt library.

   x := 1
       
     //You can check the type with Printf
     fmt.Printf("%T", x)

-Declare an invariant value with const. Usually written outside the function.

const (
    //No type specified in const (interpreted by the compiler but not executed)
	Username = "test_user"
	Password = "test_pass"
)

-When called from other go files, start the variable name with an uppercase letter.

Array

-The size of the array created by specifying the number of elements cannot be changed. -If you create an array with a slice without specifying the number of elements, you can add elements with append. -There are two ways to declare an array with 0 elements ↓

    //Pattern 1 Generate an array with initial values in memory
	array1 := make([]int, 0)
	//Pattern 2 nil(Null treatment)
	var array2 []int

Map -It is saved as a Map type. -The value can be retrieved from the key.

    m := map[string]int{"apple": 100, "banana": 200}

	//You can retrieve the value from the key
	fmt.Println(m["apple"])

	//You can specify the key and update the value
	m["banana"] = 300
	fmt.Println(m)

	//New elements can be added
	m["new"] = 500
	fmt.Println(m)

Closure

-Can be set as a return value for each function. -The value in the function is retained. -Since it is difficult to understand the movement of the value, it is better to follow the movement with a debug tool.

func circleArea(pi float64) func(radius float64) float64 {
	return func(radius float64) float64 {
		return pi * radius * radius
	}
}

func main(){
    //First set the pi
	c1 := circleArea(3.14)
    c2 := circleArea(3)

	//Execute calculation from the set pi
    fmt.Println(c1(2))
	fmt.Println(c2(2))
}

For the explanation of closures, I also referred to the here site.

Difference from Java

Since I usually use Java, there were some points I was interested in.

-There is no ";" at the end of the line. -The class does not exist. -Do not specify public / private. -Describe in the order of "variable name ". (Reverse to java) -If there is even one unused variable, an error will occur. -Handle values using functions provided by the library, not object-oriented. (There is no inheritance of object class.)

Is Go an object-oriented language? Yes and no. Although Go has types and methods and allows an object-oriented style of programming, there is no type hierarchy. The concept of “interface” in Go provides a different approach that we believe is easy to use and in some ways more general. There are also ways to embed types in other types to provide something analogous—but not identical—to subclassing. Moreover, methods in Go are more general than in C++ or Java: they can be defined for any sort of data, even built-in types such as plain, “unboxed” integers. They are not restricted to structs (classes). Also, the lack of a type hierarchy makes “objects” in Go feel much more lightweight than in languages such as C++ or Java.

Impressions

・ Processing is fast. -I felt that the function description method was similar to javaScript. (I'm not sure because I have little knowledge of javaScript ... I will also study JS.) ・ Map is easy to handle. Elements can be extracted without searching with the for statement. ・ Learning at udemy is efficient because you can move your hands while listening to the explanation.

Recommended Posts

[Go] Basic grammar ① Definition
[Go] Basic grammar ② Statement
[Go] Basic grammar ③ Pointer
Python3 basic grammar
Python basic grammar / algorithm
Python basic grammar (miscellaneous)
Python basic grammar note (4)
Python basic grammar note (3)
Python basic grammar memo
Ruby expert learned the basic grammar of Go language
Basic Python 3 grammar (some Python iterations)
Python installation and basic grammar
Python Basic Grammar Memo (Part 1)
Python basic grammar (miscellaneous) Memo (3)
Python basic grammar (miscellaneous) Memo (2)
Basic Python grammar for beginners
I learned Python basic grammar
Python basic grammar (miscellaneous) Memo (4)
Python (Python 3.7.7) installation and basic grammar
Java and Python basic grammar comparison
Introduction to Ansible Part 2'Basic Grammar'
About the basic type of Go
Basic grammar of Python3 system (dictionary)
Comparing the basic grammar of Python and Go in an easy-to-understand manner
[Basic grammar] Differences between Ruby / Python / PHP
[Python] I personally summarized the basic grammar.
Basic grammar of Python3 system (character string)
Basic grammar of Python3 series (list, tuple)
Basic grammar of Python3 system (included notation)