将以数字“1”开头的所有一、二、三个产品ID插入到输出中。

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

Insert into output all one + two + three product IDs that start with the digit "1"

问题

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

我对Go语言还不熟悉。如何将以数字"1"开头的所有one + two + three产品ID插入到output中?

package main

import (
	"fmt"
)

func main() {
	var output []string
	// 1) 将以数字"1"开头的所有one + two + three产品ID插入到output中

	fmt.Println(output)

}

func GetOneProductIDs() (out []string) {
	for i := 0; i < 100; i += 10 {
		out = append(out, fmt.Sprintf("%d_%s", i, "one"))
	}
	return out
}

func GetIwoProductIDs() (out []string) {
	for i := 0; i < 100; i += 5 {
		out = append(out, fmt.Sprintf("%d_%s", i, "two"))
	}
	return out
}

func GetThreeProductIDs() (out []string) {
	for i := 0; i < 100; i += 2 {
		out = append(out, fmt.Sprintf("%d_%s", i, "three"))
	}
	return out

}

https://play.golang.org/p/ftdSMJiSfq_D

英文:

I am new to Go. How do I insert into output all one + two + three product IDs that start with the digit "1"

package main

import (
	&quot;fmt&quot;
)

func main() {
	var output []string
	// 1) Insert into output all one + two + three product IDs that start with the digit &quot;1&quot;

	fmt.Println(output)

}

func GetOneProductIDs() (out []string) {
	for i := 0; i &lt; 100; i += 10 {
		out = append(out, fmt.Sprintf(&quot;%d_%s&quot;, i, &quot;one&quot;))
	}
	return out
}

func GetIwoProductIDs() (out []string) {
	for i := 0; i &lt; 100; i += 5 {
		out = append(out, fmt.Sprintf(&quot;%d_%s&quot;, i, &quot;two&quot;))
	}
	return out
}

func GetThreeProductIDs() (out []string) {
	for i := 0; i &lt; 100; i += 2 {
		out = append(out, fmt.Sprintf(&quot;%d_%s&quot;, i, &quot;three&quot;))
	}
	return out

}

https://play.golang.org/p/ftdSMJiSfq_D

答案1

得分: 2

我为您提供了一个相当原始和直接的解决方案,但它可以正常工作:

package main

import (
	"fmt"
	"strings"
)

func main() {

	var output []string
	// 1) 将以数字“1”开头的所有 one + two + three 产品ID插入到 output 中
	for _, res := range GetOneProductIDs() {
		if strings.HasPrefix(res, "1") {
			output = append(output, res)
		}
	}
	for _, res := range GetIwoProductIDs() {
		if strings.HasPrefix(res, "1") {
			output = append(output, res)
		}
	}
	for _, res := range GetThreeProductIDs() {
		if strings.HasPrefix(res, "1") {
			output = append(output, res)
		}
	}
	fmt.Println(output)

}

func GetOneProductIDs() (out []string) {
	for i := 0; i < 100; i += 10 {
		out = append(out, fmt.Sprintf("%d_%s", i, "one"))
	}
	return out
}

func GetIwoProductIDs() (out []string) {
	for i := 0; i < 100; i += 5 {
		out = append(out, fmt.Sprintf("%d_%s", i, "two"))
	}
	return out
}

func GetThreeProductIDs() (out []string) {
	for i := 0; i < 100; i += 2 {
		out = append(out, fmt.Sprintf("%d_%s", i, "three"))
	}
	return out
}

您可以在以下链接中查看代码:https://play.golang.org/p/RwDFEMJ-5Je

英文:

I've got pretty primitive and straightforward solution for you but it works fine:

package main

import (
	&quot;fmt&quot;
	&quot;strings&quot;
)

func main() {

	var output []string
	// 1) Insert into output all one + two + three product IDs that start with the digit &quot;1&quot;
	for _, res := range GetOneProductIDs() {
		if strings.HasPrefix(res, &quot;1&quot;) {
			output = append(output, res)
		}
	}
	for _, res := range GetIwoProductIDs() {
		if strings.HasPrefix(res, &quot;1&quot;) {
			output = append(output, res)
		}
	}
	for _, res := range GetThreeProductIDs() {
		if strings.HasPrefix(res, &quot;1&quot;) {
			output = append(output, res)
		}
	}
	fmt.Println(output)

}

func GetOneProductIDs() (out []string) {
	for i := 0; i &lt; 100; i += 10 {
		out = append(out, fmt.Sprintf(&quot;%d_%s&quot;, i, &quot;one&quot;))
	}
	return out
}

func GetIwoProductIDs() (out []string) {
	for i := 0; i &lt; 100; i += 5 {
		out = append(out, fmt.Sprintf(&quot;%d_%s&quot;, i, &quot;two&quot;))
	}
	return out
}

func GetThreeProductIDs() (out []string) {
	for i := 0; i &lt; 100; i += 2 {
		out = append(out, fmt.Sprintf(&quot;%d_%s&quot;, i, &quot;three&quot;))
	}
	return out
}

https://play.golang.org/p/RwDFEMJ-5Je

huangapple
  • 本文由 发表于 2021年7月24日 14:41:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/68507642.html
匿名

发表评论

匿名网友

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

确定