duan7772 2017-08-10 10:07
浏览 3
已采纳

为从其他包中声明的结构分配值

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条回答 默认 最新

  • doulangdang9986 2017-08-10 10:10
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 学习Python如何找兼职
  • ¥15 python结合Matlab仿真忆阻器
  • ¥35 有人会注册whatsaop协议号吗?
  • ¥15 lead dbs 无法导入影像数据
  • ¥15 多目标MPA算法优化编程实现
  • ¥15 反激PWM控制芯片调研
  • ¥15 Python for loop减少运行时间
  • ¥15 fluent模拟物质浓度udf
  • ¥15 Collection contains no element matching the predicate
  • ¥20 冻品电商平台的搜索是怎么实现的