在导入的Golang包中将map传递给函数时出现错误。

huangapple go评论79阅读模式
英文:

Error passing map to a function in imported Golang package

问题

我需要将一个地图发送到一个导入包中声明的函数,并且一直收到以下错误:

> ./main.go:21: cannot use m (type map[string]ticket) as type map[string]some_package.ticket in function argument

这是在golang GOPATH文件夹中main.go的代码:

package main

import (	
        "time"
         sp "./some_package"
        )

type ticket struct {
  Timestamp int64
  Identification_number int
}
var m map[string]ticket


func main() {
  humans := 10
  m = make(map[string]ticket)
  m["ticket1"] = ticket{time.Now().Unix(), 1234}
  sp_ := sp.New(humans)
  sp_.SetTicket(m)
}

这是在golang GOPATH文件夹中/some_package中some_package.go的代码:

package some_package

type park struct{
  card map[string]ticket
  people int
}

type ticket struct {
  Timestamp int64
  Identification_number int
}

func (p *park) SetTicket(m map[string]ticket)(){

}

func New(humans int)(p *park){
  p.card = make(map[string]ticket)
  p.people = humans
  return p
}

我应该如何将地图的值传递给导入包函数,或者如何从父包中的函数中获取地图的值?这是可能的吗,还是有解决方法?

英文:

I need to send a map to a function declared in a imported package, and keep getting this error:

> ./main.go:21: cannot use m (type map[string]ticket) as type map[string]some_package.ticket in function argument

This is the code of main.go in golang GOPATH folder:

package main

import (	
        "time"
         sp "./some_package"
        )

type ticket struct {
  Timestamp int64
  Identification_number int
}
var m map[string]ticket


func main() {
  humans := 10
  m = make(map[string]ticket)
  m["ticket1"] = ticket{time.Now().Unix(), 1234}
  sp_ := sp.New(humans)
  sp_.SetTicket(m)
}

And this is the code of some_package.go in /some_package in golang GOPATH folder:

package some_package

type park struct{
  card map[string]ticket
  people int
}

type ticket struct {
  Timestamp int64
  Identification_number int
}

func (p *park) SetTicket(m map[string]ticket)(){

}

func New(humans int)(p *park){
  p.card = make(map[string]ticket)
  p.people = humans
  return p
}

How should I pass the value of the map to the imported package function or get the map value inside the function
from the parent package?
Is this even possible or is there a workaround?

答案1

得分: 3

尽管这两种类型的名称和结构相同,但它们并不是同一种类型。类型总是属于一个包,而这两种类型属于不同的包。

因此,你不应该将some_package中的ticket类型复制粘贴到main中,而是应该采取以下两个步骤:1. 通过将其命名为大写来将其导出到其他包中:type Ticket struct;2. 在main中使用它,例如make(map[string]sp.Ticket)

英文:

Even though the two types are named the same and contain the same structure, they are not the same type. A type always belongs to a package, and those two types belong to different packages.

So you shouldn't copypaste the type ticket from some_package to main, but instead 1. export it to other packages by naming it in uppercase: type Ticket struct; and 2. use it from main like make(map[string]sp.Ticket).

huangapple
  • 本文由 发表于 2015年1月27日 07:22:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/28160603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定