How to join a slice of ints into a single string in Go

1 Answer

0 votes
package main 
  
import ( 
    "fmt"
    "strings"
    "strconv"
) 
  
func main() { 
    slice := []int{1, 5, 8, 2, 4}
    slicetxt := []string{}

    for i := range slice {
        n := slice[i]
        txt := strconv.Itoa(n)
        slicetxt = append(slicetxt, txt)
    }

    s := strings.Join(slicetxt, " + ")
    fmt.Println(s)
  
} 
  
   
 
    
/*
run:
     
1 + 5 + 8 + 2 + 4
 
*/

 



answered Aug 18, 2020 by avibootz

Related questions

1 answer 167 views
1 answer 133 views
1 answer 141 views
1 answer 136 views
1 answer 79 views
1 answer 154 views
...