golang(14)
-
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 -
go 문법 part2(반복문, 조건문, defer)
part2 For For loop문은 세개의 컴포넌트가 ; 세미콜론으로 분배되어있다. 초기화: 처음 반복하기 전에 실행된다. 조건문: 모든 반복을 하기전 조건이 맞는지 확인하고 실행한다. 말미문: 모든 반복의 끝에 실행된다. 초기화해줄 때 := 짧은 변수 선언문을 자주 사용한다. 그리고 그 변수의 scope는 반복문 안에서만 유요하다. 조건문에서의 결과가 false가 되면 멈춘다. package main import "fmt" func main() { sum := 0 for i := 0; i < 10; i++ { sum += i } fmt.Println(sum) } 그리고 초기화와 말미문은 option으로 선택할 수 있다. package main import "fmt" func main() { sum :..
2020.03.19