S
S
sergey_fs2017-10-21 18:02:27
go
sergey_fs, 2017-10-21 18:02:27

Doesn't return JSON when using goroutine?

Good afternoon. Tell me, I can’t get an error in JSON if I send all the construction
code to a single goroutine for processing

func() {
    err := add_user(name,pass)
    if err != nil {
        jsresponce, _ := json.Marshal(Error{"   User "+name+" is already exist  !!!!  "})
        io.WriteString(w, string(jsresponce))
        return
    }
}()

so it returns an error in the JSON string
if run as
go func() {
    err := add_user(name,pass)
    if err != nil {
        jsresponce, _ := json.Marshal(Error{"   User "+name+" is already exist  !!!!  "})
        io.WriteString(w, string(jsresponce))
        return
    }
}()

it does not return, can you tell me what the problem is?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2017-10-21
@sergey_fs

Because you don't wait for the goroutine to complete, you exit the json_directory function before the goroutine finishes running. Accordingly, you do not have time to see the result.
In your situation, I don’t see the point of putting it in a goroutine at all. You still have to wait for completion to show the answer, so you can just leave it as the first option.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question