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.
・ 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.
-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.)
-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))
}
-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
-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.
-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
Recommended Posts