Software Engineering Wiki

ToSnake

Converts a string to snake case.

  • Use strings.Fields() to get the words in the string, strings.Join() to combine them using "_" as the separator.
import "strings"

func ToSnake(s string) string {
	return strings.Join(strings.Fields(s), "_")
}
ToSnake("some text") // "some_text"

Last updated 15 September 2026 · Edit this page