英文:
Golang - How to iterate through two slices at the same time
问题
我正在尝试编写一个函数,该函数接受一个结构体作为参数,其中包含两个嵌套的结构体。我需要遍历这两个嵌套的结构体,找到"Service"字段,并删除由"-"分隔的前缀。
我已经编写了一个函数,它可以实现我想要的功能并删除前缀,但是它由两个for循环组成,分别遍历这两个独立的结构体。有没有办法让我以一种方式编写这个函数,使它可以在一个for循环中遍历这两个结构体?
以下是结构体的定义:
var myJson = `
{
"ID": "hgfd5432",
"Greeting": "Welcome!",
"ServiceNames": [
{
"Service": "sevice-name-service1",
"Version": "1.8"
},
{
"Service": "sevice-name-service2",
"Version": "1.8"
},
{
"Service": "sevice-name-service3",
"Version": "1.9"
},
{
"Service": "sevice-name-service4",
"Version": "0.6"
}
],
"Services": [
{
"Service": "sevice-name-service5",
"Version": "1.8"
}
],
"BusinessUnit": "Unit 1",
"Queue": "UK73_Advocacy_PCCT",
"Input": "Default",
}`
type Profile struct {
ProfileId string `json:"ID"`
Input string `json:"Input"`
ParentProfile string `json:"ParentProfile"`
Persona string `json:"Persona"`
BusinessUnit string `json:"BusinessUnit"`
Greeting string `json:"Greeting"`
Queue string `json:"Queue"`
ServiceNames []ServiceKey `json:"ServiceNames"`
Services []ServiceInfo `json:"Services"`
}
以下是该函数的代码:
func removePrefix(inputParameters *Profile) error {
for i := 0; i < len(inputParameters.ServiceNames); i++ {
a := strings.Split(inputParameters.ServiceNames[i].Service, "-")
s := a[len(a)-1]
inputParameters.ServiceNames[i].Service = s
}
for i := 0; i < len(inputParameters.Services); i++ {
a := strings.Split(inputParameters.Services[i].Service, "-")
s := a[len(a)-1]
inputParameters.Services[i].Service = s
}
return nil
}
希望对你有所帮助!
英文:
I'm trying to write a function that takes in a struct, and within that there are two nested structs. I need to iterate through both nested structs, find the "Service" field and remove the prefixes that are separated by the '-'.
I've written a function that does what I want it to and removes the prefixes, however it consists of two for loops that loop through the two separate structs. Is their a way for me to write this function in a way that it loops through the structs in one for loop?
Here are the structs:
var myJson = `
{
"ID": "hgfd5432",
"Greeting": "Welcome!",
"ServiceNames": [
{
"Service": "sevice-name-service1",
"Version": "1.8"
},
{
"Service": "sevice-name-service2",
"Version": "1.8"
},
{
"Service": "sevice-name-service3",
"Version": "1.9"
},
{
"Service": "sevice-name-service4",
"Version": "0.6"
}
],
"Services": [
{
"Service": "sevice-name-service5",
"Version": "1.8"
}
],
"BusinessUnit": "Unit 1",
"Queue": "UK73_Advocacy_PCCT",
"Input": "Default",
}`
type Profile struct {
ProfileId string `json:"ID"`
Input string `json:"Input"`
ParentProfile string `json:"ParentProfile"`
Persona string `json:"Persona"`
BusinessUnit string `json:"BusinessUnit"`
Greeting string `json:"Greeting"`
Queue string `json:"Queue"`
ServiceNames []ServiceKey `json:"ServiceNames"`
Services []ServiceInfo `json:"Services"`
And here is the function:
func removePrefix(inputParameters *Profile) error {
for i := 0; i < len(inputParameters.ServiceNames); i++ {
a := strings.Split(inputParameters.ServiceNames[i].Service, "-")
s := a[len(a)-1]
inputParameters.ServiceNames[i].Service = s
}
for i := 0; i < len(inputParameters.Services); i++ {
a := strings.Split(inputParameters.Services[i].Service, "-")
s := a[len(a)-1]
inputParameters.Services[i].Service = s
}
return nil
答案1
得分: 1
你可以这样做:
serviceNamesLength := len(inputParameters.ServiceNames)
servicesLength := len(inputParameters.Services)
maxLength := serviceNamesLength
if servicesLength > maxLength {
maxLength = servicesLength
}
for i := 0; i < maxLength; i++ {
if i < servicesLength {
// 处理服务
}
if i < serviceNamesLength {
// 处理服务名称
}
}
英文:
One way you can do this is,
serviceNamesLength := len(inputParameters.ServiceNames)
servicesLength := len(inputParameters.Services)
maxLength := serviceNamesLength
if servicesLength > maxLength {
maxLength = servicesLength
}
for i := 0; i < maxLength; i++ {
if i < servicesLength {
// services stuff
}
if i < serviceNamesLength {
// services names stuff
}
}
答案2
得分: 0
以下是使用并发方式完成此操作的代码:
func ConcurrencyLoop(inputParameters *Profile) {
done := make(chan interface{})
var wg sync.WaitGroup
wg.Add(2)
servicesNameChan := TransformServiceNames(done, inputParameters.ServiceNames, &wg)
servicesInfoChan := TransformServiceInfo(done, inputParameters.Services, &wg)
if servicesName, ok := <-servicesNameChan; ok {
inputParameters.ServiceNames = servicesName
}
if servicesInfo, ok := <-servicesInfoChan; ok {
inputParameters.Services = servicesInfo
}
close(done)
wg.Wait()
}
func TransformServiceNames(done <-chan interface{}, servicesKey []ServiceKey, wg *sync.WaitGroup) <-chan []ServiceKey {
keysChan := make(chan []ServiceKey)
go func() {
defer wg.Done()
transformedServicekeys := make([]ServiceKey, 0)
for _, serviceKey := range servicesKey {
a := strings.Split(serviceKey.Service, "-")
s := a[len(a)-1]
transformedServicekeys = append(transformedServicekeys, ServiceKey{
Service: s,
})
}
for {
select {
case <-done:
return
case keysChan <- transformedServicekeys:
}
}
}()
return keysChan
}
func TransformServiceInfo(done <-chan interface{}, servicesKey []ServiceInfo, wg *sync.WaitGroup) <-chan []ServiceInfo {
keysChan := make(chan []ServiceInfo)
go func() {
defer wg.Done()
transformedServiceinfo := make([]ServiceInfo, 0)
for _, serviceKey := range servicesKey {
a := strings.Split(serviceKey.Service, "-")
s := a[len(a)-1]
transformedServiceinfo = append(transformedServiceinfo, ServiceInfo{
Service: s,
})
}
for {
select {
case <-done:
return
case keysChan <- transformedServiceinfo:
}
}
}()
return keysChan
}
英文:
here's how you can do this,by using concurrency:
func ConcurrencyLoop(inputParameters *Profile) {
done := make(chan interface{})
var wg sync.WaitGroup
wg.Add(2)
servicesNameChan := TransformServiceNames(done, inputParameters.ServiceNames, &wg)
servicesInfoChan := TransformServiceInfo(done, inputParameters.Services, &wg)
if servicesName, ok := <-servicesNameChan; ok {
inputParameters.ServiceNames = servicesName
}
if servicesInfo, ok := <-servicesInfoChan; ok {
inputParameters.Services = servicesInfo
}
close(done)
wg.Wait()
}
func TransformServiceNames(done <-chan interface{}, servicesKey []ServiceKey, wg *sync.WaitGroup) <-chan []ServiceKey {
keysChan := make(chan []ServiceKey)
go func() {
defer wg.Done()
transformedServicekeys := make([]ServiceKey, 0)
for _, serviceKey := range servicesKey {
a := strings.Split(serviceKey.Service, "-")
s := a[len(a)-1]
transformedServicekeys = append(transformedServicekeys, ServiceKey{
Service: s,
})
}
for {
select {
case <-done:
return
case keysChan <- transformedServicekeys:
}
}
}()
return keysChan
}
func TransformServiceInfo(done <-chan interface{}, servicesKey []ServiceInfo, wg *sync.WaitGroup) <-chan []ServiceInfo {
keysChan := make(chan []ServiceInfo)
go func() {
defer wg.Done()
transformedServiceinfo := make([]ServiceInfo, 0)
for _, serviceKey := range servicesKey {
a := strings.Split(serviceKey.Service, "-")
s := a[len(a)-1]
transformedServiceinfo = append(transformedServiceinfo, ServiceInfo{
Service: s,
})
}
for {
select {
case <-done:
return
case keysChan <- transformedServiceinfo:
}
}
}()
return keysChan
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论