Last active
July 24, 2018 06:50
-
-
Save WeslyG/2c08d3ec66c3d5cdfa5b4c16e44798f1 to your computer and use it in GitHub Desktop.
Golang example
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
// build string config | |
output := strings.Join([]string{"Nodes.", config.NodeName, ".status"}, "") | |
fmt.Println(output) | |
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 ( | |
"bytes" | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
) | |
// struct for json answer from server | |
type serverResponse struct { | |
Command string `json:"command"` | |
} | |
func main() { | |
url := "http://localhost:3000/getJob" | |
jsonStr := []byte(`{"nodeName":"node01", "index": "started"}`) | |
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr)) | |
req.Header.Set("Content-Type", "application/json") // importantly | |
// init client | |
client := &http.Client{} | |
// go | |
resp, err := client.Do(req) | |
if err != nil { | |
panic(err) | |
} | |
defer resp.Body.Close() | |
// read body | |
body, readErr := ioutil.ReadAll(resp.Body) | |
if readErr != nil { | |
log.Fatal(readErr) | |
} | |
// in struct json | |
message := serverResponse{} | |
jsonErr := json.Unmarshal(body, &message) | |
if jsonErr != nil { | |
log.Fatal(jsonErr) | |
} | |
fmt.Print(message.Command) |
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
file, err := ioutil.ReadFile("/path/to/index.txt") | |
if err != nil { | |
fmt.Print(err) | |
} | |
str := string(file) | |
fmt.Println(str) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment