Last active
November 21, 2016 16:08
-
-
Save houssemFat/63dae47098e544327c781abeb7e95ffa to your computer and use it in GitHub Desktop.
Golang snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
) | |
func main() { | |
s := `{"text":"I'm a text.","number":1234,"floats":[1.1,2.2,3.3],"innermap":{"foo":1,"bar":2}}` | |
var data map[string]interface{} | |
err := json.Unmarshal([]byte(s), &data) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("text =", data["text"]) | |
fmt.Println("number =", data["number"]) | |
floats := data["floats"].([]interface{}) | |
// floats := data["floats"].([]interface{}) | |
for _, entry := range floats {= | |
fmt.Println(entry) | |
} | |
fmt.Println("floats =", data["floats"]) | |
fmt.Println("innermap =", data["innermap"]) | |
innermap, ok := data["innermap"].(map[string]interface{}) | |
if !ok { | |
panic("inner map is not a map!") | |
} | |
fmt.Println("innermap.foo =", innermap["foo"]) | |
fmt.Println("innermap.bar =", innermap["bar"]) | |
fmt.Println("The whole map:", data) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net/smtp" | |
"log" | |
"fmt" | |
"encoding/base64" | |
"encoding/json" | |
) | |
/** | |
* []string -- {"recipient@example.net"} | |
* {string} message -- | |
*/ | |
func sendMail(auth smtp.Auth, data map[string]interface{}){ | |
_recipients := data["to"].([]interface{}) | |
recipients := make([]string, len(_recipients)) | |
for index, entry := range _recipients { | |
recipients[index] = entry.(string) | |
} | |
header := make(map[string]string) | |
header["From"] ="fathallah.houssem@gmail.com" | |
header["Subject"] = "gl" | |
header["MIME-Version"] = "1.0" | |
header["Content-Type"] = "text/plain; charset=\"utf-8\"" | |
header["Content-Transfer-Encoding"] = "base64" | |
message := "" | |
for k, v := range header { | |
message += fmt.Sprintf("%s: %s\r\n", k, v) | |
} | |
_message , err := json.Marshal(data["data"]) | |
if err != nil{ | |
fmt.Println(err) | |
return | |
} | |
message += "\r\n" + base64.StdEncoding.EncodeToString([]byte(string(_message))) | |
fmt.Println(">>>>>>>>>>>>>>>>>>") | |
fmt.Println(recipients) | |
fmt.Println(">>>>>>>>>>>>>>>>>>") | |
// Connect to the server, authenticate, set the sender and recipient, | |
// and send the email all in one step. | |
err = smtp.SendMail( | |
"smtp.gmail.com" + ":25", | |
auth, | |
"fathallah.houssem@gmail.com", | |
recipients, | |
[]byte(message), | |
) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"encoding/json" | |
) | |
type Event struct { | |
Hy string `json:"hy"` | |
} | |
func main() { | |
message := `{"hy" : "ok"}` | |
event := Event{} | |
json.Unmarshal([]byte(message), &event) | |
fmt.Println(event.Hy) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment