2014-10-19 2 views
0

나는 당혹 스럽다. 나는 다음과 같은 몸에 게시 할 때대소 문자 구분이 맞습니까?

{"lng":1.23, "lat":4.56,"utc":789} 

이 하나 개의 반환 (잘못된) {0, 0, 0}

func test(rw http.ResponseWriter, req *http.Request) { 
    type data struct { 
    lng float64 
    lat float64 
    utc int 
    } 
    decoder := json.NewDecoder(req.Body) 
    var t data 
    err := decoder.Decode(&t) 
    if err != nil { 
     panic("PANIC") 
    } 
    log.Println(t) 
} 

이 하나 개를 반환 {1.23, 4.56, 789} (올바른)

func test(rw http.ResponseWriter, req *http.Request) { 
    type data struct { 
    Lng float64 
    Lat float64 
    Utc int 
    } 
    decoder := json.NewDecoder(req.Body) 
    var t data 
    err := decoder.Decode(&t) 
    if err != nil { 
     panic("PANIC") 
    } 
    log.Println(t) 
} 

유일한 차이점은 내가 구조체 정의에서 대문자를 사용하고 있다는 것입니다. 내가 빠진 것이 있습니까? 이거 버그 야?

+0

관련 질문보기 : [1] (http://stackoverflow.com/questions/26327391/golang-json-marshalstruct-returns/26327436), [2] (http://stackoverflow.com/questions/26050469/) json-unmarshall-not-working-properly/26050501), [3] (http://stackoverflow.com/questions/25595096/unmarshalling-json-golang-not-working/25595269). –

답변

11

JSON encoding packageexported fields에서만 작동합니다. 디코더는 대소 문자를 구분하지 않습니다.

package documentation에 설명 된대로 필드 태그를 사용하여 인코딩 할 때 대소 문자를 제어 할 수 있습니다.

Go 언어는 대소 문자를 구분합니다.