I am very new to programming in general let alone Go... currently I have been experimenting serving some content via HTTP, and [for reasons] I have a string that I want to store in a separate package, and serve it back to my main project via a function call. however I am getting an error (in various forms depending on how I change the code up):
"mismatched types string and func() string"
here is the 'data' package
package encodedjs
var base64EncodedJS string
func ReadEncodedJS() string {
return base64EncodedJS
}
func init() {
base64EncodedJS = "data:text/javascript;base64,largestring_blah_blah_blah=="
}
...then the main code calling it
package main
import (
"./encodedjs"
)
func main() {
js := "some text to simply show one way I've tried" + encodedjs.ReadEncodedJS
}
The encodedjs.ReadEncodedJS
qualified identifier just denotes the function, if used as-is, it is just a function value (e.g. can be assigned to variables of the same function type).
If you want to call it and use its return value, use parentheses, e.g. encodedjs.ReadEncodedJS()
. This is detailed in Spec: Calls.
Since you want to concatenate string
values, you have to call it:
js := "some text to simply show one way I've tried" + encodedjs.ReadEncodedJS()
string
is a string
and func() string
is a function that returns a string
. Looks to me that encodedjs.ReadEncodedJS
is the latter and to get the string
that it returns you would write encodedjs.ReadEncodedJS()
.
I am irritated that I forgot that I was wanting the returned result of the function and should have added the parenthetical null () at the end... I mean I worked on this for an embarrassingly long time to not have that ever click in my head... and it sits right next to other correct function calls quite literally in front of my face.