Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dictionary 在cunston类型中使用interator属性_Dictionary_Go_Iterator_Reverse_Golang Migrate - Fatal编程技术网

Dictionary 在cunston类型中使用interator属性

Dictionary 在cunston类型中使用interator属性,dictionary,go,iterator,reverse,golang-migrate,Dictionary,Go,Iterator,Reverse,Golang Migrate,我在编写Go代码时,使用basemap[string]int创建了一个类型,我需要创建一个返回映射、反转键和值的方法。我开始编写代码,但无法迭代我创建的类型 到目前为止,我已经编写了以下代码: package constants type Month map[string]int; // LongMonth is a relationship with string and date value (int) var LongMonth = Month{ "Janary":1,

我在编写Go代码时,使用basemap[string]int创建了一个类型,我需要创建一个返回映射、反转键和值的方法。我开始编写代码,但无法迭代我创建的类型

到目前为止,我已经编写了以下代码:

package constants

type Month map[string]int;

// LongMonth is a relationship with string and date value (int)
var LongMonth = Month{
    "Janary":1,
    "February":2,
    "March":3,
    "April":4, 
    "May":5,
    "June": 6,
    "July": 7,
    "August": 8,
    "September": 9,
    "Octuber": 10,
    "Novenber": 11,
    "Decenber": 12,
}

// ShortMonth is a relationship with a resume string and date value (int)
var ShortMonth = Month{
    "Jan":1,
    "Feb":2,
    "Mar":3,
    "Apr":4, 
    "May":5,
    "Jun": 6,
    "Jul": 7,
    "Aug": 8,
    "Sep": 9,
    "Oct": 10,
    "Nov": 11,
    "Dec": 12,
}

func (m* Month) Reverse() map[int]string {
    n:=make(map[int]string);
    for k, v := range m {
        n[v] = k
    }
    return n
};
// LongMonthReverse is a relationship with string and date value (int)
// var LongMonthReverse = reverseMonth(LongMonth);
// ShortMonthReverse is a relationship with string and date value (int)
// var ShortMonthReverse = reverseMonth(ShortMonth);

我需要函数Reverse返回Reverse emonth。例如:month={Jan:1…”Dec:12}和month.Reverse()返回{1:“Jan”…12:“Dec”}

您不能迭代指针,或者将
func(m*month)Reverse()map[int]string
的方法接口更改为
func(m month)Reverse()map[int]string
,或者您需要在
Reverse()的内部使用
*m


您不能迭代指针,或者将
func(m*Month)Reverse()map[int]string
的方法接口更改为
func(m Month)Reverse()map[int]string
,或者您需要在
Reverse()的内部使用
*m


您不能使用
m
作为范围的值,因为其类型是
*月份
。我建议将该方法的签名更改为
func(m Month)Reverse()map[int]string
您不能将
m
用作范围的值,因为其类型为
*Month
。我建议将方法的签名更改为
func(m月)Reverse()map[int]string
package main

import "fmt"


type Month map[string]int

// LongMonth is a relationship with string and date value (int)
var LongMonth = Month{
    "Janary":1,
    "February":2,
    "March":3,
    "April":4,
    "May":5,
    "June": 6,
    "July": 7,
    "August": 8,
    "September": 9,
    "Octuber": 10,
    "Novenber": 11,
    "Decenber": 12,
}

// ShortMonth is a relationship with a resume string and date value (int)
var ShortMonth = Month{
    "Jan":1,
    "Feb":2,
    "Mar":3,
    "Apr":4,
    "May":5,
    "Jun": 6,
    "Jul": 7,
    "Aug": 8,
    "Sep": 9,
    "Oct": 10,
    "Nov": 11,
    "Dec": 12,
}

func (m* Month) Reverse() map[int]string {
    n:=make(map[int]string)
    // this is the fix
    for k, v := range *m {
        n[v] = k
    }
    return n
}


func main() {
  fmt.Println(ShortMonth.Reverse())
}