golang/golang-grammar(9)
-
goroutine deadlock
deadlock deadlock이란 다른 두 작업이 서로 종료될 때까지 기다리다가 결국엔 아무것도 못하는 상태이다. channel을 사용하다 보면 가장 중요하게 고려해볼만 한 것이 Deadlock이다. 만약 goroutine이 channel을 통해 data를 보내게 되고 그러고 나서 다른 goroutine으로 부터 데이터를 받기를 기다리고 있다. 하지만 값을 받지 못하게 된다면 deadlock이 발생하게 됩니다. 비슷하게 groutine이 channel로 부터 값을 받기 기다린다고 몇몇의 다른 goroutine channel을 통해 data를 쓰는 것을 기대하게된다. 그러면 panic이 발생한다. package main func main() { ch := make(chan int) ch
2020.04.01 -
goroutine and channel
goroutine이란 go routine은 함수 및 메소드를 다른 함수 및 메소드와 동시에 사용할 수 있게 해준다. go routine은 상당히 가벼운 thread이다. thread와 비교하면 만들고 사용하는데 적은 비용이든다. 그러므로 go application은 몇천개의 goroutine이 몇천개 동시에 돌아가고 있습니다. goroutine의 장점 goroutine은 thread와 비교하여 적은 비용이 든다. goroutine은 몇 kb이며 goroutine은 thread에 비해 application의 요청에 따라 stack의 용량을 늘렸다 줄일 수 있다. thread의 경우 stack사이즈가 고정되어 있다. goroutine들은 몇개의 os thread에 다중송신한다. 몇 천개의 goroutine으..
2020.04.01 -
Slice and Array
이전에 정리한 적이 있지만 조금 부족한 것 같아 아래의 사이트를 통해 정리하려고 합니다. https://blog.golang.org/slices-intro Go Slices: usage and internals - The Go Blog Andrew Gerrand 5 January 2011 Introduction Go's slice type provides a convenient and efficient means of working with sequences of typed data. Slices are analogous to arrays in other languages, but have some unusual properties. This article will look at wha blog.gola..
2020.03.30 -
go 문법 part5 (goroutine, channel, Select)
Part5 Goroutines gorutines는 go runtime에 의한 가벼운 스레드 관리입니다. go f(x, y, z) f, x, y, z는 최근 go routine에서 실행된다. f의 실행은 새로운 goroutine에서 일어난다. Goroutines는 같은 주소 공간에서 실행된다. 그래서 메모리에 접근할 때 반드시 동기화하여 접근하여야 한다. sync package는 유용한 primitives를 제공합니다. 하지만 다른 primitive가 있기 때문에 go에서는 필요하지 않을 것입니다. go의 경우 해당 함수가 유요할 때만 실행하게 됩니다. package main import ( "fmt" "time" ) func say(s string) { for i := 0; i < 5; i++ { tim..
2020.03.27 -
golang part4(Method, Pointer and Method, Interface, Stringers, Error, Readers)
part4 Method Go는 class를 갖고 있지 않습니다. 그러나 타입과 위에 method를 정의할 수 있습니다. method는 특별한 receiver arguments를 갖고 있는 함수입니다. receiver는 자신의 arguments를 func와 method keyword 사이에 작성된다. package main import ( "fmt" "math" ) type Vertex struct { X, Y float64 } func (v Vertex) Abs() float64 { return math.Sqrt(v.X*v.X + v.Y*v.Y) } func main() { v := Vertex{3, 4} fmt.Println(v.Abs()) } 일반적인 함수 선언, 호출과의 차이점은 v.Abs()를 사..
2020.03.24 -
go 문법 part3(pointer, struct, array, slice, range, map)
part3 Pointer Go는 C언어에 있는 Pointer를 사용할 수 있다. 이러한 표기법으로 사용할 수 있다. *T T는 value이다. 선언만 한 변수에 들어있는 값은 nil이다. var p *int &연산자를 사용하면 위와 같이 변수를 만들지 않고도 pointer 변수를 선언할 수 있다. i:=42 p:=&i 연산자 표시는 포인터가 가리키는 값을 가져온다. fmt.Println(*p) *p = 21 package main import "fmt" func main() { i, j := 42, 2701 p := &i fmt.Println(*p) *p = 21 fmt.Println(i) p = &j *p = *p / 37 fmt.Println(j) } structs struct는 필드들의 모음입니다...
2020.03.20