[Go] Basic grammar ② Statement

I've finished watching the "statement" section of the udemy course, so I'll summarize and look back.

This time as well, I learned the basic operation while outputting 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]](https://www.udemy.com/course/go-fintech/) on udemy During ~

・ Development environment is VS Code + plug-in (Go version is 1.15) ・ OS is Windows ・ I usually use Java.

if statement / switch statement

-The writing method is similar to java. -If you put parentheses in the conditional statement in the if statement, an error will occur. (Even if you write parentheses as a habit, VS Code will automatically remove the parentheses.)

for statement

-The for statement can be written in the same way as java. ・ It is easier to describe by using "range". -"Range" can also be used in map.

Comparing my own answers to the exercises given in the course with sample answers, I found the difference between a description like java and a description using range. Both give the same result, but using range is cleaner. Since range can also handle maps, it seems to be useful for applications.


Problem: Write the code to find the smallest number from the slices below and output it.

l := []int{100, 300, 23, 11, 23, 2, 4, 6, 4}

My answer


 l := []int{100, 300, 23, 11, 23, 2, 4, 6, 4}
    min := l[0]
    for i := 0; i < len(l); i++ {
        if min >= l[i] {
            min = l[i]
        }
    }
 fmt.Println(min)

Answer example


 l := []int{100, 300, 23, 11, 23, 2, 4, 6, 4}
 
    var min int
 
    for i, num := range l {
        if i == 0 {
            min = num
            continue
        }
 
        if min >= num {
            min = num
        }
    }
 
 fmt.Println(min)

defer -The processing described in defer is executed after the processing of the function is completed. -Convenient to use for closing files. -Executed in a stack manner. (The process described at the beginning is executed at the end.)

Example: Output the contents of the file to the console


package main

import (
	"fmt"
	"os"
)

func main() {
    file, _ := os.Open("./lesson.go")
	//The file must always be closed. If you write it in defer first, you will not forget to close it.
	defer file.Close()

	//When reading a file, it is necessary to prepare a byte array as data.
	data := make([]byte, 100)
	file.Read(data)
	//Cast to string
	fmt.Println(string(data))

}

log

-The standard library (log) can only output the minimum log. -If you need detailed logs, use a third-party library. -In Fatalln (), the processing is forcibly terminated at that point, so subsequent processing is not performed.

Typical log output function


    log.Println("logging!")
    log.Printf("%T %v", "test", "test")
	
	log.Fatalln("error!") //At this point the court is finished.
    log.Fatalf("%T %v", "test", "test") //Not executed

Error handling

-Handle with if instead of try-catch. -It is not recommended to describe panic (exception) in the program. It is recommended to handle the received error with an if statement.

Impressions

-Convenient because you can perform common processing (array operation, file close, log output) with a short description. -It is difficult because you must understand the behavior of the function and the recommended description accurately. -There seems to be a limit to the functions in the standard library for performing complicated processing.



Last post

Basic Grammar ① Definition

Recommended Posts

[Go] Basic grammar ② Statement
[Go] Basic grammar ① Definition
[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
Python basic if statement
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)