使用结构体、映射或其他什么?

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

Use structs , maps or something else?

问题

我是你的中文翻译助手,以下是翻译好的内容:

我对Go语言(以及编程)都是新手,所以采用了边学边练的方法。

对于下面的代码(用于获取天气预报),我希望从我拥有的列表中填充经度和纬度。

我不太确定如何首先设置该列表 - 使用结构体,然后将存储的结构体的值传递给这段代码?我尝试过这样做,但似乎只能在一个结构体中保存一个经度/纬度对。我考虑使用映射,但无法弄清楚如何设置所有内容。也许,我应该将值列表存储在文本文件中并从那里读取:

p := Place{
	placeName: "Accra", lat: "43.6595", long: "-79.3433",
	placeName: "Kumasi", lat: "43.6595", long: "-79.3433",
	placeName: "Tamale", lat: "43.6595", long: "-79.3433",
}

f, err := forecast.Get(key, p.lat, p.long, "now", forecast.CA)
if err != nil {
	log.Fatal(err)
}

结构体看起来像这样(我有大约10个不同的经度/纬度值要使用):

type Place struct {
	placeName string
	lat       string
	long      string
}
英文:

I am new to Go ( and programming to an extent), so using a learn as you practice approach.

For the code below ( which pulls weather forecast ), I am looking to populate the longitude and latitude from a list I have.

I am not entirely sure how to setup that list in the first place - use structs and then pipe the value of the stored struct into this code? I tried that and it seems I can only hold one lon/lat pair in a struct. I was thinking of using maps but couldn't figure out how to set it all up. Perhaps, I should store the list of values in a text file and read from there:

p := Place{
	placeName: "Accra", lat: "43.6595", long: "-79.3433",
	placeName: "Kumasi", lat: "43.6595", long: "-79.3433",
	placeName: "Tamale", lat: "43.6595", long: "-79.3433",
}

f, err := forecast.Get(key, p.lat, p.long, "now", forecast.CA)
if err != nil {
	log.Fatal(err)
}

the struct looked like this ( I have about 10 separate long/lat values to use)

type Place struct {
	placeName string
	lat       string
	long      string
}

答案1

得分: 0

Place结构体是一个单一的对象,它有三个属性,即名称、纬度和经度。为了拥有多个对象,你需要逐个实例化它们,并且通常会将它们存储在一个集合中。在Go语言中,合理的选择是使用数组、切片或映射。

这里是一个使用切片的示例。数组的语法大致相同。如果你愿意,我可以在午餐后提供一个使用映射的示例。还有一件事需要注意,我使用了一种称为"复合字面量"的语法来分配集合。还有其他的方法可以做到这一点...我可以使用append方法或更常规的索引/键赋值,比如myArray[0] = instanceOfPlaceThatIsAlreadyInitialized

places := []Place{
	Place{placeName: "Accra", lat: "43.6595", long: "-79.3433"},
	Place{placeName: "Kumasi", lat: "43.6595", long: "-79.3433"},
	Place{placeName: "Tamale", lat: "43.6595", long: "-79.3433"},
}

扩展示例:https://play.golang.org/p/EHBgn_I9dA

编辑:使用映射的相同示例:

places := map[string]Place{
	"Accra": Place{placeName: "Accra", lat: "43.6595", long: "-79.3433"},
	"Kumasi": Place{placeName: "Kumasi", lat: "43.6595", long: "-79.3433"},
	"Tamale": Place{placeName: "Tamale", lat: "43.6595", long: "-79.3433"},
}

https://play.golang.org/p/1A45-Z6k7q

英文:

The Place struct is a single object, it has three properties, a name and lat and long. In order to have more than one you have to instantiate them individually, and commonly, you'd store them in a collection. In Go, the sensible choices would be an array, slice, or map.

Here is an example using a slice. The syntax for array is roughly the same. If you'd like I can provide an example that uses a map after I return from lunch. One other thing to note is that I'm allocating the collection using a syntax referred to as 'composite literal' there are other ways to do this... I could use the append method or more conventional assignment to indexes/keys like myArray[0] = instanceOfPlaceThatIsAlreadyInitialized.

places := []Place{
		Place{placeName: "Accra", lat: "43.6595", long: "-79.3433"},
		Place{placeName: "Kumasi", lat: "43.6595", long: "-79.3433"},
		Place{placeName: "Tamale", lat: "43.6595", long: "-79.3433"},
	}

Extended example; https://play.golang.org/p/EHBgn_I9dA

EDIT: Same thing with a map;

places := map[string]Place{
		"Accra":Place{placeName: "Accra", lat: "43.6595", long: "-79.3433"},
		"Kumasi":Place{placeName: "Kumasi", lat: "43.6595", long: "-79.3433"},
		"Tamale": Place{placeName: "Tamale", lat: "43.6595", long: "-79.3433"},
	}

https://play.golang.org/p/1A45-Z6k7q

huangapple
  • 本文由 发表于 2015年9月18日 23:52:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/32656237.html
匿名

发表评论

匿名网友

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

确定