package main
import (
"fmt"
)
type one struct {
name string
}
func action(s string) {
have := s
fmt.Println(have)
want := one{}
fmt.Println(want)
}
func main() {
s := `one{}`
action(s)
}
It’s totally not clear to me what you want to do exactly. Do you want to select a struct type based on the string value given (i.e., a factory of some kind)? A switch is one way:
var want interface{} // this is a bad smell
switch s {
case "one":
want = one{}
case "two":
want = two{}
default:
panic("wat")
}
You need to figure out an appropriate type for want. The type interface{} that I used above can be used to hold anything, but in return you can do almost nothing with it.
I am sending in json to an API and I’d like to get the response back and use it. At the moment I’d need to move the r := sendRespose{} line to the original function, but I’m wondering if I can work around that.
// SendSMS sends a text with the users phone number
func (c *Client) SendSMS(message string, to ...string) error {
// Endcoding:(0 => 7-bit, 1 => 8-bit, 2 => UCS2)
var data = &send{
Recipients: to,
Message: message,
Encoding: 0,
SmsConnectorID: 0,
Store: true,
AuthToken: c.AuthToken,
APIKey: c.APIKey,
}
url := EndPointURL + "remote/sms/send"
req, err := buildIt(data, url)
if err != nil {
panic(err)
}
fmt.Println("send response:", req)
// confirmSent(req)
return nil
}
func buildIt(data interface{}, url string) (sendRespose, error) {
send, err := json.Marshal(data)
if err != nil {
panic(err)
}
fmt.Println("send sms", string(send))
req, err := http.NewRequest("POST", url, bytes.NewBuffer(send))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(reflect.TypeOf(body))
// //Response status and body for quick testing
// fmt.Println("Status:", resp.Status)
// fmt.Println("Body:", string(body))
r := sendRespose{}
_ = json.Unmarshal(body, &r)
fmt.Println(r)
if r.ErrorCode != 0 {
return r, ErrorCode{
time.Now(),
r.ErrorCode,
}
}
return r, nil
}