본문 바로가기

go34

[Go] Structure Structure consists of different types of values. The syntax is following: type STRUCT_NAME struct { FIELD_NAME DATA_TYPE ... } If the name of a structure starts with a capital letter, it is exported to the outside of the package. Structure Declaration var testStruct struct { numField float64 strField string boolField bool } fmt.Println(testStruct) // {0 false}] testStruct.numField = 7 testStruct.. 2021. 10. 11.
[Go] Array An array is a collection of values all of which have the same type. The value that an array has is called an element. The syntax of array definition is following: var VARIABLE_NAME [NUMBER_OF_ELEMENTS]DATA_TYPE var numArr [3]int numArr[0] = 1 numArr[1] = 2 numArr[2] = 3 fmt.Println(numArr[0], numArr[1], numArr[2]) // 1 2 3 An array is static. Therefore, it is not possible to add a new element af.. 2021. 10. 10.
[Go] Control Flow Like the other languages, Go has a similar syntax for control flow. Conditional Statement if Statement if CONDITION { STATEMENTS } else if CONDITION { STATEMENTS } else { STATEMENTS } Definitely, you know, if statements are executed only if the conditions are true. else if and else statements can be omitted. In addition, if statement can be nested. package main import "fmt" func main() { score :.. 2021. 10. 10.
[Go] Constant A constant is an unchanged value. Only primitive type can be defined as constant. Boolean Rune Integer Real Number Complicated Number String The type that is not a primitive type like Structure, Array can't be defined as constant. Constant Definition const Name Type = Value If the name starts with a capital letter, the constant is exposed to out of the file. Constant Usage package main func main.. 2021. 10. 5.
[Go] The Error of Floating Point Number Like most programming languages, Go also has the floating point error problem. Problem Please look at the below code. package main import ( "fmt" ) func main() { a := 0.1 b := 0.2 c := a + b fmt.Println(c) } // 0.30000000000000004 c was not a 0.3! Therefore, we are hard to compare floating point numbers. a := 0.1 b := 0.2 c := 0.3 d := a + b if c == d { fmt.Println("It's the same!") } else { fmt.. 2021. 10. 4.
[Go] Operator Operator Precedence Unary operators have the highest precedence. As the ++ and -- operators form statements, not expressions, they fall outside the operator hierarchy. Go supports only post-increment and post-decrement operators, not pre-increment and pre-decrement. There are five precedence levels for binary operators. Binary operators of the same precedence associate from left to right. Preced.. 2021. 10. 4.
[Go] Package fmt Package fmt implements formatted I/O with functions analogous to C's printf and scanf. Description General %v : the value in a default format (when printing structs, adds the plus symbol to field names ) %#v : a Go-syntax representation of the value %T : a Go-syntax representation of the type of value Integer %b : base 2 %d : base 10 %x : base 16, with lower-case letters for a-f %X : base 16, wi.. 2021. 10. 4.
[Go] File handling File Creating file, err := os.Open(filename) if err != nil { log.Fatal(err) } File Reading scanner := bufio.NewScanner(file) for scanner.Scan() { fmt.Println(scanner.Text()) } Scan() method reads one line from the file. Text() method returns the content of the line as a string. File Closing err = file.Close() if err != nil { log.Fatal(err) } Example package main import ( "bufio" "fmt" "log" "os".. 2020. 12. 2.
[Go] Package os Package os provides a platform-independent interface to operating system functionality. The os interface is intended to be uniform across all operating systems. Features not generally available appear in the system-specific package syscall. Constants O_RDONLY : read-only O_WRONLY : write-only O_RDWR : read-write O_APPEND : append data when writing O_CREATE : create if none exists O_EXCL : file m.. 2020. 12. 2.
[Go] Package testing Package testing provides support for automated testing of Go packages. Methods Error Error is equivalent to Log() followed by Fail(). func (c *T) Error(args ...interface{}) Errorf Errorf is equivalent to Logf() followed by Fail(). func (c *T) Errorf(format string, args ...interface{}) Usage package main import "testing" func TestFunction(t *testing.T) { t.Error("Test Failed") } 2020. 11. 30.
[Go] Package log Package log implements a simple logging package. Methods Fatal Fatal is equivalent to Print() followed by a call to os.Exit(1). func Fatal(v ...interface{}) Fatalf Fatalf is equivalent to Printf() followed by a call to os.Exit(1). func Fatalf(format string, v ...interface{}) Fatalln Fatalln is equivalent to Println() followed by a call to os.Exit(1). Panic Panic is equivalent to Print() followed.. 2020. 11. 30.
[Go] Configure VSCode Currently, I am using VSCode. I've used it since the beta. So I develop most of the language with VSCode. This post describes the configuring development environment of Go in VSCode. Extension First, install an extension for Go. If you search Go in the VSCode extension, you will see the two kinds of extensions. Go is a stable version as an LTS, and Go Nightly is a preview version under developme.. 2020. 11. 28.