2019-01-17 16:59
浏览 261

类型“ string”和“ func()string”之间有什么区别?

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 ( 
func main() {
 js := "some text to simply show one way I've tried" + encodedjs.ReadEncodedJS

图片转代码服务由CSDN问答提供 功能建议

我对编程非常陌生,更不用说Go ...目前,我一直在尝试通过HTTP提供一些内容 ,并且(由于某些原因)我有一个字符串,我想将其存储在单独的程序包中,然后通过函数调用将其提供给我的主项目。\无论如何,我都会收到错误消息(根据我更改方式的不同,格式可能不同) 代码):


这是“数据”包 \ n

var base64EncodedJS字符串
func ReadEncodedJS()字符串{
func init(){
 base64EncodedJS =“ data:text / javascript; base64,largestring_blah_blah_blah ==”  


  package main 
  “ ./encodedjs” 
func main(){
 js:=“一些文本只是简单地展示了我尝试过的一种方式” + encodejs.ReadEncodedJS 
   \  n 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dp20011 2019-01-17 17:04

    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()
    打赏 评论
  • doume1301 2019-01-17 17:04

    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().

    打赏 评论
  • duanlong4890 2019-01-17 17:22

    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.

    打赏 评论

相关推荐 更多相似问题