我的代码从终端读取输入,并将这些值发送到 nats ,而它需要具有http端点。
它单独起作用,但是当我将它们全部合并时,它不会从 nats 中读取。如果您能指出正确的方向,我将不胜感激。
package main
import (
"bufio"
"fmt"
nats "github.com/nats-io/nats.go"
"html/template"
"log"
"net/http"
"os"
)
func main() {
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
tmpl := template.Must(template.ParseFiles(wd + "/template/main.html"))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data := TodoPageData{
PageTitle: "Demo",
}
tmpl.Execute(w, data)
})
http.ListenAndServe(":8081", nil)
type message struct {
content string
}
var messages []message
nc, err := nats.Connect(
nats.DefaultURL,
)
if err != nil {
log.Fatal(err)
}
defer nc.Close()
// Subscribe
if _, err := nc.Subscribe("updates", func(m *nats.Msg) {
fmt.Printf("Received a message: %s\n", string(m.Data))
}); err != nil {
log.Fatal(err)
}
// io r/w
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
if err := nc.Publish("updates", []byte(scanner.Text())); err != nil {
log.Fatal(err)
}
messages = append(messages, message{scanner.Text()})
for _, message := range messages {
fmt.Println(message.content)
}
}
if scanner.Err() != nil {
// handle error.
}
}
答案 0 :(得分:0)
const theNumOne = [
{
date: "monday",
time: ["06.00", "07.00", "08.00", "09.00", "10.00"]
},
{
date: "tuesday",
time: ["06.00", "07.00", "08.00", "09.00", "10.00"]
},
{
date: "wednesday",
time: ["06.00", "07.00", "08.00", "09.00", "10.00"]
}
];
const pickOne = [
{
date: "monday",
time: ["09.00", "10.00"]
},
{
date: "wednesday",
time: ["06.00", "07.00", "08.00", "09.00"]
}
];
// get theNumOne date and time, pickOne date and time, this function iterates the theNumOne array get's the
// first record, iterates the pickOne array and founds it's date key match,
// sends the theNumOne date and time, and the matched record from pickOne, to remove the unwanted time
const iterateArray = (theNumOne, pickOne) => {
theNumOne.map((pointer, index) => {
const extractedRecord = pickOne.find( key => key.date === pointer.date );
purifyingArray(theNumOne[index], extractedRecord);
})
};
const purifyingArray = (theNumOne, extractedRecord) => {
if(extractedRecord !== undefined) {
extractedRecord.time.map((pointer) => {
let index = theNumOne.time.indexOf(pointer);
if(index > -1) {
theNumOne.time.splice(index,1);
}
});
}
console.log(theNumOne);
};
iterateArray(theNumOne, pickOne);
是阻止呼叫。在新的goroutine上启动它:
http.ListenAndServe