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 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库