从Golang中的参数调用const

时间:2018-10-23 11:03:23

标签: go

所以我要使我的项目具有这样的项目集

 GoTraining
   -Controllers
       ListController
   -Service
       ListService that doing business process and
       calling data Access Object (DAO) to get data
   -DAO
       List query and Model(Const)
   -.gitignore
   -config.conf
   -main.go
   -readme.md

那只是我的结构的图片,现在到了我困惑的地方 我在dao中创建一个名为customerDao.go的程序包,并将所有查询写入 const 内,然后尝试创建一个名为queryFilter的函数,并使用queryType作为参数,我想基于queryType搜索const。由于某种原因我得到了参数,它给了我一条错误消息,说

“语法错误:意外的const,期望表达式(8,16)” enter image description here

谁能告诉我我在这里做错了吗?

1 个答案:

答案 0 :(得分:1)

您似乎假设queryGetAllCustomer“属于” const。尽管Const不是作用域,但仅是访问修饰符,这两个代码片段产生的结果相同:

const (
    queryGetAllCustomer = "..."
    queryGetOneCustomer = "..."
)

// and...

const queryGetAllCustomer = "..."
const queryGetOneCustomer = "..."

因此,您可以简单地使用queryGetAllCustomer(或如果导出了其他软件包中的dao.QueryGetAllCustomer)来引用值:

queryText := queryGetAllCustomer