Fill Dropdown and get Strings of Textfield of Website from Go

Hello Guys,

i currently try to do a Fileserver for my Project.
i have to fill a Drop-down-menu with a struct-array, get the name back of the chosen one of the box and have to read the strings inside a textfield of HTML from Go and i dont know how to do that. Pls send me some Code Example cause im new to this language and dont understand much.

Here is my Go-Code:

    //Klimakammer struct
type klimakammer struct {
	name        string
	Hersteller  string
	ip          string
	sollTemp    string
	istTemp     string
	sollFcht    string
	istFcht     string
	kammerstart bool
	kammerstop  bool
}

//Ct01 Klimakammern erstellen
var Ct01 = klimakammer{"ct01", "weiss", "10.0.62.22", "", "", "", "", false, true}

//Kammern - Fill Klimakammer Array
var Kammern = []klimakammer{
	Ct01,
}

func main() {

	fs := http.FileServer(http.Dir("./static"))
	http.Handle("/", fs)

	http.ListenAndServe(":8080", nil)

	http.HandleFunc("/getKammer", func(w http.ResponseWriter, r *http.Request) {
        //The Struct-Array kammern should be passed here to the DropDown
	})

}

And here is my Func.js Code: -> it’s an onClick Function when you start the program per button

    function fillChoiceBox() {
    var x = document.getElementById("Kammer"); // ID of DropDown
    //Loop for Struct Array
    var option = document.createElement("option");
    option.text = //Every single Chamber/kammer as option like Ct01.name
    x.add(option);
}

And that was my previous try, cause of the HandleFunc “/getKammer”

// $.ajax({
//     url: "http://localhost:8080/getKammer",
//     method: "GET",
//     function(data) {
//         var $dropdown = $("#Kammer");
//         $dropdown.append($("<option />").val(data).text(this.name));
//         $("#getKammer").prop('disabled', true);
//         $dropdown.append("<option>hallooo</option>")
//         document.getElementById("status").value = "es funktioniert yee";
//         },
//     });

Encode it to JSON:

package main

import (
	"encoding/json"
	"fmt"
)

type Klimakammer struct {
	Name        string `json:"name"`
	Hersteller  string `json:"hersteller"`
	Ip          string `json:"ip"`
	SollTemp    string `json:"sollTemp"`
	IstTemp     string `json:"istTemp"`
	SollFcht    string `json:"sollFcht"`
	IstFcht     string `json:"istFcht"`
	Kammerstart bool   `json:"kammerstart"`
	Kammerstop  bool   `json:"makkerstop"`
}

var Ct01 = Klimakammer{"ct01", "weiss", "10.0.62.22", "", "", "", "", false, true}

var Kammern = []Klimakammer{
	Ct01,
}

func main() {
	buff, _ := json.Marshal(&Kammern)

	fmt.Println(string(buff))
}

Output:

[{
	"name": "ct01",
	"hersteller": "weiss",
	"ip": "10.0.62.22",
	"sollTemp": "",
	"istTemp": "",
	"sollFcht": "",
	"istFcht": "",
	"kammerstart": false,
	"makkerstop": true
}]

see https://play.golang.org/p/w_v_Ujg2ZZd

Note that only exported fields can be serialized like this!

and how to get it into the ChoiceBox?

Write the JSON string to the w http.ResponseWriter in the handler func.

Client side JS is out of scope for this forum.

okay thanks

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.