본문 바로가기

golang17

golang에서 for loop scoping go 1.22버전에서 for loop 내의 변수 범위에 대한 업데이트가 있어서 공식 블로그에 올라온 글을 번역하면서 정리해보고자 한다. 관련 문서 https://go.dev/blog/loopvar-preview https://go.googlesource.com/proposal/+/master/design/60078-loopvar.md 문제 go로 코드를 짜면 다음과 같이 실수를 저지르는 경우가 많을 것이다. func main() { done := make(chan bool) values := []string{"a", "b", "c"} for _, v := range values { go func() { fmt.Println(v) done 2023. 10. 2.
Go 상속 vs 구성 golang의 객체지향은 일반적인 다른 언어들과 다르다. golang은 명시적으로 상속의 개념이 없다. 대신 composition으로 상속을 대체한다. golang에서의 상속 개념을 정리해두고자 한다. Struct golang은 c++과 유사한 부분이 많다. 공식적으로 클래스라는 개념이 존재하지 않고, struct로 class를 구현하는 점에서 그러하다. type Person struct { name string age int } func(p Person) greeting() { // greeting 함수 Person구조체에 연결 fmt.Println("Hello") } type Student struct { p Person // 학생 구조체 안에 사람 구조체를 필드로 가지고 있음( Has - a ) s.. 2023. 10. 1.
Go context 파헤치기 go 언어에는 다른 언어에는 없는 context라는 개념이 존재한다. context라는 생소한 개념을 한번 정리해보고자 한다. 참고 문서 https://pkg.go.dev/context https://go.dev/blog/context What is context? 작업을 지시할 때 deadline, cancel 시그널, 또는 request-scope의 변수들을 전송할 수 있는 객체이다. 모든 Context는 root로 Background Context를 가지는 tree 구조로 되어 있다. 부모 Context의 속성을 물려받아 새로운 Context를 생성할 수 있다. Context는 struct type 내에 저장되면 안된다. 대신, 명시적으로 필요로하는 function에 context를 넘겨주어야 한다.. 2023. 9. 29.
goroutine 파헤치기 Go 언어에서는 내부적으로 고루틴이라는 경량 쓰레드를 사용해서 Concurrency를 구현한다. 하지만 그동안은 정확히 쓰레드와 어떤 차이가 있고 어떻게 고루틴이 동작하는지 정확히 알지 못한 채 사용하기만 해서 이번 기회에 이를 정리해두고자 한다. 참고 문서 https://blog.nindalf.com/posts/how-goroutines-work/ https://syntaxsugar.tistory.com/entry/GoGolang-Scheduler https://ssup2.github.io/theory_analysis/Golang_Goroutine_Scheduling/ https://www.ardanlabs.com/blog/2018/08/scheduling-in-go-part2.html Goroutin.. 2023. 9. 23.