Go Lang custom convert array into string with two separators This is my custom function to join strings from an array into one string.
We can provide normal separator and the last one. JoinStringsIntoArray(&quot;, &quot;, &quot; and &quot;, words...)
func JoinStringsIntoArray(sep string, lastSep string, words ...string) string {
lastElementIndex := len(words) - 1
var joinedString string

for index, word := range words[0:lastElementIndex] {
    if index == lastElementIndex-1 {
        sep = lastSep
    }
    joinedString += word + sep
}

joinedString += words[lastElementIndex]

    return joinedString
}

Test for this function
func TestStringsJoinsWithCommas(t *testing.T) {
    var words []string
    words = append(words, &quot;one&quot;, &quot;two&quot;, &quot;three&quot;)

    expectedResult := &quot;one, two and three&quot;
    result := JoinStringsIntoArray(&quot;, &quot;, &quot; and &quot;, words...)

    if expectedResult != result {
        t.Errorf(&quot;Strings joiner is broken, it should be '%v', we got '%v'&quot;, expectedResult, result)
    }
}

What do you think about this, can this solution be improved? The function works fine but I'm not so sure it's written well.
The Go standard library strings package has a Join function:
func Join(elems []string, sep string) string

Your function is a simple extension of strings.Join:
func JoinLast(elems []string, sep, lastSep string) string

However, for no obvious reason, you chose a different function parameter signature:
func JoinLast(sep string, lastSep string, words ...string) string

We need a comment to explain why this isn't an arbitrary or idiosyncratic decision.

Your testing is inadequate. Check boundary conditions. Your function fails on an empty words list.
words = []string{}


You have not provided any testing benchmarks. For example,
BenchmarkJoin-4   5534918  207.9 ns/op   104 B/op  5 allocs/op

func JoinStringsIntoArray(sep string, lastSep string, words ...string) string {
    lastElementIndex := len(words) - 1
    var joinedString string

    for index, word := range words[0:lastElementIndex] {
        if index == lastElementIndex-1 {
            sep = lastSep
        }
        joinedString += word + sep
    }

    joinedString += words[lastElementIndex]

    return joinedString
}

func BenchmarkJoin(b *testing.B) {
    words := []string{&quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;, &quot;five&quot;}
    b.ReportAllocs()
    b.ResetTimer()
    for N := 0; N &lt; b.N; N++ {
        JoinStringsIntoArray(&quot;, &quot;, &quot; and &quot;, words...)
    }
}


You can do better. For example, minimize allocation,
BenchmarkJoin-4  16612879   70.95 ns/op   32 B/op  1 allocs/op

func JoinLast(elems []string, sep, lastSep string) string {
    var join strings.Builder
    joinCap := 0
    for i := 0; i &lt; len(elems); i++ {
        joinCap += len(elems[i])
    }
    if len(elems) &gt; 1 {
        joinCap += len(lastSep)
        if len(elems) &gt; 2 {
            joinCap += (len(elems) - 2) * len(sep)
        }
    }
    join.Grow(joinCap)

    last := len(elems) - 1 - 1
    for i, elem := range elems {
        if i &gt;= last {
            if i == last {
                sep = lastSep
            } else {
                sep = &quot;&quot;
            }
        }
        join.WriteString(elem)
        join.WriteString(sep)
    }

    return join.String()
}

func BenchmarkJoin(b *testing.B) {
    words := []string{&quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;, &quot;five&quot;}
    b.ReportAllocs()
    b.ResetTimer()
    for N := 0; N &lt; b.N; N++ {
        JoinLast(words, &quot;, &quot;, &quot; and &quot;)
    }
}