Hey. There is a reader:

func (r *GzipLineReader) ReadItem(item interface{}) error { if !r.Scanner.Scan() { return io.EOF } fmt.Println(reflect.TypeOf(item)) err := json.Unmarshal(r.Scanner.Bytes(), &item) fmt.Println(reflect.TypeOf(item)) if err != nil { return err } return nil } 

The structure is passed as an argument.

 type ThesaurusItem struct { Id int ObjectType ObjectNodeGraphType ThesaurusId int `json:"id"` Keywords []string `json:"keywords"` Type string `json:"type"` } 

The scanner is associated with a file in which json `s are written line by line:

 {"id":466,"type":"ΠšΠ½ΠΈΠΆΠ½Ρ‹ΠΉ ΠΌΠ°Π³Π°Π·ΠΈΠ½","keywords":["ΠΊΠ½ΠΈΠ³ΠΈ","ΠΆΡƒΡ€Π½Π°Π»Ρ‹","ΠΊΠ°Ρ€Ρ‚Ρ‹","ΠΊΡƒΠΏΠΈΡ‚ΡŒ ΠΊΠ½ΠΈΠ³Ρƒ","ΠΊΠ½ΠΈΠΆΠ½Ρ‹ΠΉ","ΠΊΠ½ΠΈΠ³Π°","ΠΊΠ½ΠΈΠ³ΠΈ","ΠΊΠ½ΠΈΠ³Π΅","ΠΊΠ½ΠΈΠ³Ρƒ","ΠΊΠ½ΠΈΠ³ΠΎΠΉ","книгою","ΠΊΠ½ΠΈΠ³","ΠΊΠ½ΠΈΠ³Π°ΠΌ","ΠΊΠ½ΠΈΠ³Π°ΠΌΠΈ","ΠΊΠ½ΠΈΠ³Π°Ρ…","ΠΆΡƒΡ€Π½Π°Π»","ΠΆΡƒΡ€Π½Π°Π»Π°","ΠΆΡƒΡ€Π½Π°Π»Ρƒ","ΠΆΡƒΡ€Π½Π°Π»ΠΎΠΌ","ΠΆΡƒΡ€Π½Π°Π»Π΅","ΠΆΡƒΡ€Π½Π°Π»Ρ‹","ΠΆΡƒΡ€Π½Π°Π»ΠΎΠ²","ΠΆΡƒΡ€Π½Π°Π»Π°ΠΌ","ΠΆΡƒΡ€Π½Π°Π»Π°ΠΌΠΈ","ΠΆΡƒΡ€Π½Π°Π»Π°Ρ…","ΠΊΠ°Ρ€Ρ‚Π°","ΠΊΠ°Ρ€Ρ‚Ρ‹","ΠΊΠ°Ρ€Ρ‚Π΅","ΠΊΠ°Ρ€Ρ‚Ρƒ","ΠΊΠ°Ρ€Ρ‚ΠΎΠΉ","ΠΊΠ°Ρ€Ρ‚ΠΎΡŽ","ΠΊΠ°Ρ€Ρ‚","ΠΊΠ°Ρ€Ρ‚Π°ΠΌ","ΠΊΠ°Ρ€Ρ‚Π°ΠΌΠΈ","ΠΊΠ°Ρ€Ρ‚Π°Ρ…","ΠΊΠ½ΠΈΠΆΠ½Ρ‹ΠΉ","ΠΊΠ½ΠΈΠΆΠ½ΠΎΠ³ΠΎ","ΠΊΠ½ΠΈΠΆΠ½ΠΎΠΌΡƒ","ΠΊΠ½ΠΈΠΆΠ½Ρ‹ΠΌ","ΠΊΠ½ΠΈΠΆΠ½ΠΎΠΌ","книТная","ΠΊΠ½ΠΈΠΆΠ½ΠΎΠΉ","ΠΊΠ½ΠΈΠΆΠ½ΡƒΡŽ","книТною","ΠΊΠ½ΠΈΠΆΠ½ΠΎΠ΅","ΠΊΠ½ΠΈΠΆΠ½Ρ‹Π΅","ΠΊΠ½ΠΈΠΆΠ½Ρ‹Ρ…","ΠΊΠ½ΠΈΠΆΠ½Ρ‹ΠΌΠΈ","ΠΊΠ½ΠΈΠΆΠ½","ΠΊΠ½ΠΈΠΆΠ½Π°","ΠΊΠ½ΠΈΠΆΠ½ΠΎ","ΠΊΠ½ΠΈΠΆΠ½Ρ‹"]} 

In the console, I have:

ThesaurusItem

map [string] interface {}

I do not understand the reasons for this behavior. Why does interface become a map? How to achieve the state in which the empty interface, after executing the Unmarshall function, will be filled with the data interface?

    1 answer 1

    Here is the code that shows what you are doing wrong: https://play.golang.org/p/asqKup5tN0 . json.Unmarshal and similar functions expect a pointer to a value packed in an empty interface. You give it a pointer to an empty interface, in which lies the value. In order for your code to work, pass a pointer to the ReadItem method, and to json.Unmarshal is just an interface that came to you:

     func (r *GzipLineReader) ReadItem(item interface{}) error { // ... err := json.Unmarshal(r.Scanner.Bytes(), item) // ... } // ... err := glr.ReadItem(&item) 

    In addition, follow the official guidelines ( Id should be ID ) and do not forget about gofmt (the code of your structure is not readable). And to print the type of a variable, use fmt.Printf("%T", v) .

    • What do you mean by json.Unmarshal ΠΈ ΠΏΠΎΠ΄ΠΎΠ±Π½Ρ‹Π΅ Π΅ΠΌΡƒ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ ? - hedgehogues
    • You do not know why such an implementation of json.Unmarshal was made exactly? - hedgehogues
    • As for the gofmt, I, of course, use it. But in this case I copied the structure from the code where there were comments that I deleted here on the stack, so gofmt did not apply. ID correct, thanks. - hedgehogues
    • @hedgehogues "Similar" - almost any functions for working with serialization. For example, encoding/xml . And what does β€œexactly such” implementation mean? You give a pointer to an object of a particular type - you fill it in; give a pointer to an empty interface - get the default value. For JSON objects, this is map[string]interface{} . - Ainar-G