Convert string to array of integers in golang

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

Convert string to array of integers in golang

问题

我正在寻找一个互联网上的答案,但没有一个符合我的需求。

我有一个字符串,格式如下:

  1. "[2,15,23]"

我试图将其转换为以下格式:

  1. [2,15,23]

我需要在转换后将其类型更改为[]int。我需要进行转换,因为我想稍后使用它来创建一个接口类型,以便在 SQL 查询中将其作为参数使用。

有没有办法进行转换?

谢谢

英文:

I am looking for an answer in internet but it no one that fit my needs.

I have a string in the following format:

  1. "[2,15,23]"

and i am trying to convert it in this format:

  1. [2,15,23]

I need the type after the convertion to change to []int. I need to convert it because i want to use it later to make an interface type to use it in a sql query as param.

Is there any way to convert it?

Thanks

答案1

得分: 7

使用json.Unmarshal的更好方法:

  1. func main() {
  2. str := "[2,15,23]"
  3. var ints []int
  4. err := json.Unmarshal([]byte(str), &ints)
  5. if err != nil {
  6. log.Fatal(err)
  7. }
  8. fmt.Printf("%v", ints)
  9. }

使用json.Unmarshal函数可以将JSON字符串解析为Go语言中的数据结构。在上述代码中,我们将字符串[2,15,23]解析为一个整数切片ints。如果解析过程中出现错误,我们使用log.Fatal函数打印错误信息并终止程序。最后,我们使用fmt.Printf函数打印解析后的整数切片ints

英文:

A better way using json.Unmarshal:

  1. func main() {
  2. str := "[2,15,23]"
  3. var ints []int
  4. err := json.Unmarshal([]byte(str), &ints)
  5. if err != nil {
  6. log.Fatal(err)
  7. }
  8. fmt.Printf("%v", ints)
  9. }

答案2

得分: 1

以下是翻译好的内容:

这是一个工作示例,如果你还没有根据评论中的建议使用json.Unmarshal进行解析,可以参考一下。

Playground链接

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "encoding/json"
  6. )
  7. func main() {
  8. str := "[2,15,23]"
  9. ints, err := convertToIntSlice(str)
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. fmt.Printf("%v", ints)
  14. }
  15. type IntSlice struct {
  16. Ints []int
  17. }
  18. func convertToIntSlice(s string) ([]int, error) {
  19. a := &IntSlice{}
  20. err := json.Unmarshal([]byte(`{"Ints":`+s+`}`), a)
  21. return a.Ints, err
  22. }

希望对你有帮助!

英文:

Here is a working example if you haven't figured it out based on the comments suggesting that you use <a href="https://golang.org/pkg/encoding/json/#Unmarshal">json.Unmarshal</a>

<a href="https://play.golang.org/p/t42PPlZXZX">Playground link</a>

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;log&quot;
  5. &quot;encoding/json&quot;
  6. )
  7. func main() {
  8. str := &quot;[2,15,23]&quot;
  9. ints, err := convertToIntSlice(str)
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. fmt.Printf(&quot;%v&quot;, ints)
  14. }
  15. type IntSlice struct {
  16. Ints []int
  17. }
  18. func convertToIntSlice(s string) ([]int, error) {
  19. a := &amp;IntSlice{}
  20. err := json.Unmarshal([]byte(`{&quot;Ints&quot;:`+s+&quot;}&quot;), a)
  21. return a.Ints, err
  22. }

答案3

得分: 1

这有点巧妙,但你可以将其视为一个CSV文件处理:

  1. import (
  2. "encoding/csv"
  3. "strings"
  4. )
  5. in := `"[2,15,23]"`
  6. r := csv.NewReader(strings.NewReader(in))
  7. records, err := r.ReadAll()
英文:

This is slightly hackish but you could treat it like a csv:

  1. import (
  2. &quot;encoding/csv&quot;
  3. &quot;strings&quot;
  4. )
  5. in := `&quot;[2,15,23]&quot;`
  6. r := csv.NewReader(strings.NewReader(in))
  7. records, err := r.ReadAll()

huangapple
  • 本文由 发表于 2017年2月7日 02:00:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/42074316.html
匿名

发表评论

匿名网友

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

确定