While many articles discuss using Go Arrays with Javascript, I am working on something unique. My goal is to read a configuration file using Go, which has server-side access, and incorporate it into a javascript function that will be rendered with the template. This approach helps avoid hard coding values in the JavaScript:
My objective is to move away from manually specifying prefixes in the code and instead store them in a separate file, eliminating the need for recompilation every time a prefix needs to be added:
javaString += "function isValidPrefix() {"
javaString += "forbidden_prefixes = [ \"1\", \"2\", \"3\", \"4\", \"5\", \"6\" ];"
... more javascript ...
javaString += "}"
Therefore, I attempted the following:
var configArr []string
configArr = LoadFile("/conf.dat")
javaString += "forbidden_prefixes = [];"
for _, eachline := range configArr {
javaString += "forbidden_prefixes.push(\" + eachline + \");"
fmt.Println(eachline)
}
Although eachLine
prints out correctly within the loop, forbidden_prefixes
contains only one element + eachLine +
. This seems to trigger a syntax error, and even when attempting to retrieve the DOM element's value for comparison, the web console indicates that the element does not exist. Previously, everything functioned correctly with hardcoded values. Am I making an error, or is this approach simply unattainable?