英文:
Assign a value to a struct which was declare from other package
问题
这是我的代码。
我将我的结构体OperatInfo提取到struct.go中,并希望在worker.go中的main包中使用这个结构体。
struct.go
package batch
type OperatInfo struct {
eventId string
hallId string
userId string
operating string
operatingID string
ip string
}
worker.go
package main
import (
"time"
"fmt"
"strconv"
"./kernel/api"
"./kernel/db"
"./batch/basic"
"./batch/struct"
)
var operatInfo batch.OperatInfo
func BatchDeposit(eventId string, userId string, hallId string, operating string, operatingID string, ip string) {
// 我在这里遇到了一个错误
operatInfo.eventId = eventId
operatInfo.hallId = hallId
operatInfo.userId = userId
operatInfo.operating = operating
operatInfo.operatingID = operatingID
operatInfo.ip = ip
}
我只是无法设置operatInfo的字段。
有任何建议或提示将会有所帮助。谢谢。
英文:
Here is my code.
I extract my struct OperatInfo to the struct.go and wanna to use this struct in the main package which in worker.go.
struct.go
package batch
type OperatInfo struct {
eventId string
hallId string
userId string
operating string
operatingID string
ip string
}
worker.go
package main
import (
"time"
"fmt"
"strconv"
"./kernel/api"
"./kernel/db"
"./batch/basic"
"./batch/struct"
)
var operatInfo batch.OperatInfo
func BatchDeposit(eventId string, userId string, hallId string, operating string, operatingID string, ip string) {
// I get an error here
operatInfo.eventId = eventId
operatInfo.hallId = hallId
operatInfo.userId = userId
operatInfo.operating = operating
operatInfo.operatingID = operatingID
operatInfo.ip = ip
}
I just can't set operatInfo fields.
Any suggestions or tips will helps. Thanks.
答案1
得分: 1
只有以大写字母开头的字段才是公开可见的。
要解决您的问题,您可以为每个字段创建getter和setter,或者按照以下方式重命名字段的结构:
type OperatInfo struct {
EventId string
HallId string
UserId string
Operating string
OperatingID string
Ip string
}
英文:
Only the fields that starts with an upper case letter are public visible.
To solve your problem you can create getter and setter for each field or rename your fields' struct as follow:
type OperatInfo struct {
EventId string
HallId string
UserId string
Operating string
OperatingID string
Ip string
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论