golang一行代码将切片转成以分号分隔的字符串

1、将int切片转成以分号分隔的字符串

package main

import(
    "fmt"
    "strings"
)

func main() {

    slice := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}

    str := strings.Replace(strings.Trim(fmt.Sprint(slice), "[]"), " ", ";", -1)

    fmt.Println("string:", str)

}

输出:

1;2;3;4;5;6;7;8;9

 

2、将string切片转成以分号分隔的字符串

package main

import(
    "fmt"
    "strings"
)

func main() {

    slice := []string{"peer0.org1.example.com","peer1.org1.example.com"}

    str := strings.Replace(strings.Trim(fmt.Sprint(slice), "[]"), " ", ";", -1)

    fmt.Println("string:", str)

}

输出:

peer0.org1.example.com;peer1.org1.example.com

 

你可能感兴趣的:(Golang)