将字符串(键)映射到数字(或任何数据)

问题描述 投票:-1回答:1

在JavaScript中,我可以执行以下操作:

const periods = {
  'mercury': 0.2408467,
  'venus': 0.61519726,
  'earth': 1.0,
  'mars': 1.8808158,
  'jupiter': 11.862615,
  'saturn': 29.447498,
  'uranus': 84.016846,
  'neptune': 164.79132,
};

const planetName = prompt();
const period = periods[planetName];

如何在go-lang中做类似的事情?

go switch-statement maping
1个回答
1
投票

您接近回答自己的问题。 :)

只需将这些标签放在Google上即可。

package main

import (
    "fmt"
)

func main() {
    periods := map[string]float32{
        "mercury": 0.2408467,
        "venus":   0.61519726,
    }

    fmt.Println(periods["mercury"])
}
© www.soinside.com 2019 - 2024. All rights reserved.