본문 바로가기

struct3

Empty struct - 출처: https://dave.cheney.net/2014/03/25/the-empty-struct - Introduction empty struct란 말 그대로 field가 하나도 없는 struct type 이다. type Q struct{} var q struct{} 근데 여기서 한 가지 의문점이 생긴다. field도 없고 데이터도 포함되어있지 않은 struct를 왜 사용하는것인가? - Width empty struct를 알아보기 전에 width에 대해서 알아보자. width 라는 용어는 gc 컴파일러에서 유래했는데, type의 instance를 저장할 때 필요한 byte 수라고 할 수 있다. 이 블로그의 원문을 쓴 필자는 프로세스의 주소 공간은 1차원이기 때문에 width가 size보다 더 적절.. 2022. 6. 21.
Go - reflection - 출처: https://quii.gitbook.io/learn-go-with-tests/go-fundamentals/reflection - 개요 reflect package에 대해 알아본다. struct x을 받고 fn을 호출해서 재귀적으로 내부의 string field들을 모두 순회하는 함수 walk(x interface{}, fn func(string))를 작성해보자. 이번 챕터에서는 이 요구사항을 구현하기위해 reflection을 사용해볼것이다. - interface 여태까지는 string, int 형 처럼 Go에서 제공하거나, BankAccount와 같이 형을 정의한 type-safety만 다루었다. 그래서 참조할 문서를 쉽게 찾을 수 있었고, 만약 잘못된 형을 함수에 넘기면 컴파일러가 이를 미.. 2022. 1. 15.
Go - struct, method, interface 출처: https://quii.gitbook.io/learn-go-with-tests/go-fundamentals/structs-methods-and-interfaces - 개요 Go에서 관련있는 데이터를 연관시켜 표현할 수 있는 struct에 대하여 알아본다. structd의 method와 interface를 통한 다형성을 알아본다. - struct example 사각형의 너비와 높이가 주어졌을 때 둘레를 구하는 코드를 작성해보자. func TestPerimeter(t *testing.T) { got := Perimeter(10.0, 10.0) want := 40.0 if got != want { t.Errorf("got %.2f want %.2f", got, want) } } Perimeter를 구현.. 2021. 12. 24.