본문 바로가기

golang16

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.
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.
Formatting in golang golang은 C-style의 포맷팅을 지원한다. 하지만 좀 더 고수준의 언어이기 때문에 좀 더 다양한 타입에 대한 출력을 지원한다. 이에 golang에서의 출력 포맷을 정리해볼 필요를 느껴서 정리해 본다. 참고: https://pkg.go.dev/fmt General %vthe value in a default format when printing structs, the plus flag (%+v) adds field names %#va Go-syntax representation of the value %Ta Go-syntax representation of the type of the value %%a literal percent sign; consumes no value %v 인스턴스 출력 {1 .. 2023. 9. 3.