I
I
Ivan2021-03-24 10:01:25
go
Ivan, 2021-03-24 10:01:25

How to return the result from the inner function to the outer one (details inside)?

Good afternoon. I am making a job parser from the site. Made just like everywhere else. First http.Get(), then goquery.NewDocumentFromReader(res.Body), and doc.Find()by class and tag. But in each vacancy there is also a link like "Learn more", and there is still information I need to hide. I came up with in a cycle when it bypasses these vacancies, in each vacancy go to this internal link, do it http.Get(), goquery.NewDocumentFromReader(res.Body)and get what I need from there. But there was a problem. The variable inside this second traversal is not visible to the main function (in this case City). How to make everyone outside understand that this is a variable? return doesn't help.

605ae3acc59cc408024164.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2021-03-24
@Djonson86

Read what scope is https://golangs.org/variable-scope
In order for the city variable (variables are usually called with a small letter) to be visible inside Each (line 76), you must declare the variable inside the same function, and not in a nested one.
For example, on line 87, write
var city string
or, as you commented out:
city := ""
Inside the nested function, you will no longer use :=, but simply =
Because equal with a colon creates a new variable , and you just need to assign a value to an existing variable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question