假設我有以下內容:一個結構type MyStructure struct { Field1 int CityNames []string}-a 類型,我用作響應。我創建這種類型只是為了在閱讀時使響應比一段字符串更具暗示性type CityNamesReponse []string然后我有一個函數,我想從我的結構中獲取名稱并將其放入響應中func GetCities() *CityNamesReponse{ dbResult := MyStructure{ Field1: 1, CityNames: []string{"Amsterdam", "Barcelona"}, } return &CityNameResponse{ dbResult.CityNames}}我不想循環數據,只想一口氣完成。也試過:return &CityNameResponse{ ...dbResult.CityNames}可以這樣做,但我是 Go 新手,有點困惑,想以正確的方式做。這感覺不太好: // This works c := dbResults.CityNames response := make(CityNameResponse, 0) response = c return &response謝謝
如何從 Go 中的字符串列表初始化類型、字符串切片
慕田峪7331174
2022-06-06 17:10:37