8-3: 정규표현식, map[string]interface{}

JJeong·2021년 1월 27일
0

The difference between those two types is just what it seems:
interface{} is the "any" type, since all types implement the interface with no functions.
map[string]interface{} is a map whose keys are strings and values are any type.
When unmarshaling a byte array from JSON into memory it's easiest to use the interface{} type, since it can store any type of JSON document (objects, arrays, primitives, etc.); however, it may require more reflection to handle the underlying data. Using a map[string]interface{} is common when you know the JSON document is an object and []interface{} is common when you know the document is an array.
However, the best approach for unmarshaling JSON - especially when you know the structure of the documents ahead of time - is to define and use custom struct types that describe the data exactly. This way you can avoid any reflection and improve the legibility of your code.

JSON에서 memory로 byte array를 unmarshaling할 때, 만일 어떤 형식으로 바뀔지 모른다면 interfact{}로 받는 게 편하다. 왜냐하면 빈 interface 타입은 모든 형태를 담을 수 있기 때문이다. 그러나 데이터를 다루려면 보다 구체적인 형식을 알아야 한다. 가장 좋은 방법은 map[string]interface{}와 interface{} 사이에서 고르기보다 데이터에 딱 맞는 형식을 커스텀하여 받는 것이다.


  • *httptest.ResponseRecorder 타입에 대하여
type ResponseRecorder struct {
    // Code is the HTTP response code set by WriteHeader.
    //
    // Note that if a Handler never calls WriteHeader or Write,
    // this might end up being 0, rather than the implicit
    // http.StatusOK. To get the implicit value, use the Result
    // method.
    Code int
   
    // HeaderMap contains the headers explicitly set by the Handler.
    // It is an internal detail.
    //
    // Deprecated: HeaderMap exists for historical compatibility
    // and should not be used. To access the headers returned by a handler,
    // use the Response.Header map as returned by the Result method.
    HeaderMap http.Header

    // Body is the buffer to which the Handler's Write calls are sent.
    // If nil, the Writes are silently discarded.
    Body *bytes.Buffer

    // Flushed is whether the Handler called Flush.
    Flushed bool
    // contains filtered or unexported fields
}

fmt.Println(rec) => &{400 map[Content-Type:[application/json; charset=UTF-8]] {"code":0,"details":null,"message":"it's NOT mobile phone number"}
false map[Content-Type:[application/json; charset=UTF-8]] true}

0개의 댓글