それが可能になります!ここで
はゴーコード:
Objetive-Cに従うとして実装されて
type NetworkingClient struct {}
func CreateNetworkingClient() *NetworkingClient {
return &NetworkingClient {}
}
type Callback interface {
SendResult(json string)
}
func (client NetworkingClient) RequestJson (countryCode string, callback Callback) {
go func() {
safeCountryCode := url.QueryEscape(countryCode)
url := fmt.Sprintf("someApi/%s", safeCountryCode)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
//Handle error
}
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
//Handle error
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
callback.SendResult(string(b))
}()
}
:
- (void)start {
...
EndpointNetworkingClient* client = EndpointCreateNetworkingClient();
[client requestJson:countryCode callback:self];
}
//Receives the json string from Go.
- (void)sendResult:(NSString*)json{
NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding];
id jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
[self handleResponse:jsonDictionary];
}