Skip to content

strings: Join drops trailing empty strings #67604

@jsumners

Description

@jsumners

Go version

1.22

Output of go env in your module/workspace:

Whatever go.dev/play has.

What did you do?

https://go.dev/play/p/lRtKx16rXk1

package main

import (
	"fmt"
	"strings"
)

func main() {
	lines := []string{
		"line 1",
		"",
		"line 2",
		"",
		"line 3",
		"",
	}

	joined := strings.Join(lines[0:4], "\n")

	foundCount := 0
	for _, c := range joined {
		if c == 0x0a {
			foundCount += 1
		}
	}

	fmt.Printf("found %d line feeds, expected 4", foundCount)
}

What did you see happen?

strings.Join skipped adding the intended final joining character.

What did you expect to see?

strings.Join would join all elements passed to it with the provided joining character.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions