본문 바로가기

go34

[Go] Testing Unit Testing Unit testing is a software testing method by which individual units of source code. This is a basic syntax for testing in Go. import "testing" func TestBlahBlah(t *testing.T) { // test statements } The test code needs to import the testing package. The test function takes a parameter as "t *testing.T". The test function's name has to start with Test. Let's make a simple program to t.. 2021. 10. 26.
[Go] goroutine and channel A goroutine is a lightweight thread in Go. Goroutine We usually use goroutine in those cases. When one task can be split into different segments to perform better. When making multiple requests to different API endpoints. Any work that can utilize multi-core CPUs should be well optimized using goroutines Running background operations in a program might be a use case for a goroutine. We simply ad.. 2021. 10. 23.
[Go] Error Handling Error Type error type is an interface including Error() method. type error interface { Error() string } In other words, we can use any type that has Error() method as error type. defer A defer statement defers the execution of a function until the surrounding function returns. Therefore, we can use it for error handling. func test() { defer fmt.Println("Apple") fmt.Println("Banana") fmt.Println(.. 2021. 10. 22.
[Go] Map Map is a collection that accesses a value through a key. In addition, map is a mutable, duplicable, and unordered data structure. Map Declaration var numMap1 map[string]int = make(map[string]int) numMap1["one"] = 1 numMap1["two"] = 2 fmt.Println(numMap1) // map[one:1 two:2] numMap2 := make(map[int]string) numMap2[1] = "one" numMap2[2] = "two" fmt.Println(numMap2) // map[1:one 2:two In Array and .. 2021. 10. 21.
[Go] Linked List Go provide some useful data structure. Usage package main import ( "container/list" "fmt" ) func main() { v := list.New() testList1 := v.PushBack(1) testList2 := v.PushFront(2) v.InsertBefore(3, testList1) v.InsertAfter(4, testList2) for e := v.Front(); e != nil; e = e.Next() { fmt.Print(e.Value, " ") } fmt.Println() // 2 4 3 1 for e := v.Back(); e != nil; e = e.Prev() { fmt.Print(e.Value, " ") .. 2021. 10. 20.
[Go] Functions Go also has functions. A function is a group of codes that perform a single action. In such a way, a function increases the reusability of code. A function has its own scope. So the variables inside a function are only used in the function. Syntax func FUNCTION_NAME(PARAMETER_NAME PARAMETER_TYPE, ...) [RETURN_NAME] RETURN_TYPE { FUNCTION_BODY } Here are some examples. func sayHi() { fmt.Println(.. 2021. 10. 19.
[Go] Interface An interface is an abstract concept which enables polymorphism in Go. An interface type is defined as a set of method signatures. A value of interface type can hold any value that implements those methods. An interface increases decoupling so helps you write clean code. Interface Declaration An interface is declared as a type. type INTERFACE_NAME interface{ METHOD_PROTOTYPES } The zero value of .. 2021. 10. 17.
[Go] Method A Method is a kind of function. But a method belongs to the receiver. In contrast, function belongs nowhere. Therefore, a method increases the cohesion of code. Method Definition func (RECEIVER) METHOD_NAME() RETURN_TYPE { STATEMENTS } A receiver can be all local types, and local type is defined as type keyword within the package. If a receiver exists, it is a method, and if not, it is a functio.. 2021. 10. 17.
[Go] Slice Slice is a scalable collection type, unlike Array. Slice has advantages over Array in terms of memory usage and speed. Slice Declaration var slice []string slice = make([]string, 3) slice[0] = "hello" slice[2] = "hi" fmt.Println(slice[0], slice[1], slice[2]) // hello hi numSlice := make([]int, 3) numSlice[0] = 1 numSlice[1] = 2 numSlice[2] = 3 fmt.Println(numSlice[0], numSlice[1], numSlice[2]) /.. 2021. 10. 16.
[Go] Package A package is a bundle of structures, constants, variables, functions, etc. with the same purpose. Using Package If you want to use packages, use import keyword. import "fmt" You can alias to package. import htemplate "html/template" You can import packages even not used. import _ "math" In this case, you don't use the package, but you can get the effects of initializing the package. Go Module Th.. 2021. 10. 16.
[Go] Rune and String A string is a set of characters and a string is an immutable. Syntax A rune can be represented by '(single quote) and it means single character. Go supports UTF-8 as a standard character code. package main import "fmt" func main() { fmt.Printf("%c\n", 'G') fmt.Printf("%c\n", '고') fmt.Printf("%c\n", '去') } A string can be represented by "(double-quote) or `(backquote or backtick or grave). A spec.. 2021. 10. 16.
[Go] Pointer Pointer is a type that has a memory address as a value. Pointer Variable Defining a pointer variable uses a * symbol in front of the type. * symbol is also used to access the value of a pointer variable. And & symbol means the address of a variable. package main import "fmt" func main() { var pNum *int num := 7 // Assign address to pointer pNum = &num // Access to pointer fmt.Println(*pNum) // 7.. 2021. 10. 11.