Skip to main content
TopMiniSite

Back to all posts

How to Split A String By A Delimiter In Golang?

Published on
4 min read
How to Split A String By A Delimiter In Golang? image

Best Go Programming Books to Buy in October 2025

1 Learning Go: An Idiomatic Approach to Real-World Go Programming

Learning Go: An Idiomatic Approach to Real-World Go Programming

BUY & SAVE
$45.95 $65.99
Save 30%
Learning Go: An Idiomatic Approach to Real-World Go Programming
2 Go Programming Language, The (Addison-Wesley Professional Computing Series)

Go Programming Language, The (Addison-Wesley Professional Computing Series)

BUY & SAVE
$28.29 $49.99
Save 43%
Go Programming Language, The (Addison-Wesley Professional Computing Series)
3 100 Go Mistakes and How to Avoid Them

100 Go Mistakes and How to Avoid Them

BUY & SAVE
$39.16 $69.99
Save 44%
100 Go Mistakes and How to Avoid Them
4 Mastering Go: Leverage Go's expertise for advanced utilities, empowering you to develop professional software

Mastering Go: Leverage Go's expertise for advanced utilities, empowering you to develop professional software

BUY & SAVE
$29.27 $54.99
Save 47%
Mastering Go: Leverage Go's expertise for advanced utilities, empowering you to develop professional software
5 Network Programming with Go: Code Secure and Reliable Network Services from Scratch

Network Programming with Go: Code Secure and Reliable Network Services from Scratch

BUY & SAVE
$34.82 $49.99
Save 30%
Network Programming with Go: Code Secure and Reliable Network Services from Scratch
6 GO Programming in easy steps: Learn coding with Google's Go language

GO Programming in easy steps: Learn coding with Google's Go language

BUY & SAVE
$13.39 $15.99
Save 16%
GO Programming in easy steps: Learn coding with Google's Go language
7 Black Hat Go: Go Programming For Hackers and Pentesters

Black Hat Go: Go Programming For Hackers and Pentesters

  • MASTER GO PROGRAMMING FOR HACKING AND PENTESTING SKILLS!
  • HANDS-ON TECHNIQUES FOR REAL-WORLD CYBERSECURITY CHALLENGES.
  • DURABLE PAPERBACK FOR EASY REFERENCE AND ON-THE-GO LEARNING!
BUY & SAVE
$30.83 $44.99
Save 31%
Black Hat Go: Go Programming For Hackers and Pentesters
8 Microservices with Go: The expert's guide to building secure, scalable, and reliable microservices with Go

Microservices with Go: The expert's guide to building secure, scalable, and reliable microservices with Go

BUY & SAVE
$35.99 $44.99
Save 20%
Microservices with Go: The expert's guide to building secure, scalable, and reliable microservices with Go
9 System Programming Essentials with Go: System calls, networking, efficiency, and security practices with practical projects in Golang

System Programming Essentials with Go: System calls, networking, efficiency, and security practices with practical projects in Golang

BUY & SAVE
$27.36 $41.99
Save 35%
System Programming Essentials with Go: System calls, networking, efficiency, and security practices with practical projects in Golang
10 Head First Go

Head First Go

BUY & SAVE
$47.42 $65.99
Save 28%
Head First Go
+
ONE MORE?

In Golang, you can split a string by a delimiter using the strings package. Here is a general approach to split a string:

  1. Import the strings package:

import "strings"

  1. Use the Split function from the strings package to split the string:

str := "Hello,World,How,Are,You" delimiter := "," result := strings.Split(str, delimiter)

In this example, the Split function takes two arguments: the string you want to split (str) and the delimiter you want to use for splitting (delimiter). In this case, the delimiter is a comma (,).

  1. The Split function will return a slice of substrings. You can then use this slice to access the individual elements that were separated by the delimiter:

for _, element := range result { // Do something with each element }

In this loop, each element of the result slice will be printed or processed as needed. The underscore (_) is used to ignore the index of the element, as we are only interested in the values.

That's how you can split a string by a delimiter in Golang using the strings package.

What is the function to split a string by a custom delimiter in Golang?

In Golang, the function Split() from the strings package can be used to split a string by a custom delimiter.

The syntax to split a string using a custom delimiter is: strings.Split(str, delimiter)

Here is an example of splitting a string by a custom delimiter:

package main

import ( "fmt" "strings" )

func main() { str := "Hello,World!This,is,a,string" delimiter := ","

parts := strings.Split(str, delimiter)

// Displaying the splitted parts
for \_, part := range parts {
	fmt.Println(part)
}

}

Output:

Hello World!This is a string

In the above example, the strings.Split() function is used to split the string str by the delimiter ,. The parts of the split string are then displayed using a for loop.

What is the function to split a string and convert each substring to an integer in Golang?

In Golang, you can use the strings.Split() function to split a string into substrings and then use the strconv.Atoi() function to convert each substring to an integer.

Here's an example of how you can do this:

package main

import ( "fmt" "strconv" "strings" )

func main() { str := "10 20 30 40 50" substrings := strings.Split(str, " ")

var numbers \[\]int
for \_, sub := range substrings {
	num, err := strconv.Atoi(sub)
	if err != nil {
		fmt.Printf("Error converting substring '%s' to int: %v\\n", sub, err)
		continue
	}
	numbers = append(numbers, num)
}

fmt.Println(numbers)

}

This program splits the input string "10 20 30 40 50" into substrings using the space as the delimiter, and then converts each substring to an integer using strconv.Atoi(). The resulting integers are stored in the numbers slice.

What is the purpose of the TrimSpace function in Golang?

The TrimSpace function in Golang is used to remove leading and trailing white spaces from a string. Its purpose is to clean up and normalize strings by removing any excess spaces that may cause inconsistencies or issues in further processing or comparisons. It is commonly used in data validation or when processing user input.

How to split a string and convert each substring to lowercase in Golang?

To split a string and convert each substring to lowercase in Go, you can use the strings.Split() function to split the string into an array of substrings, and then loop through the array and use the strings.ToLower() function to convert each substring to lowercase.

Here's an example:

package main

import ( "fmt" "strings" )

func main() { str := "Hello World! This is a TEST String."

// Splitting the string based on whitespace
substrings := strings.Split(str, " ")

// Converting each substring to lowercase
for i, sub := range substrings {
	substrings\[i\] = strings.ToLower(sub)
}

// Printing the modified substrings
for \_, sub := range substrings {
	fmt.Println(sub)
}

}

In this example, we start by defining the original string as str. Then we use strings.Split() to split the string into an array of substrings based on whitespace. Next, we loop through the array of substrings and use strings.ToLower() to convert each substring to lowercase.

Finally, we print each modified substring using a separate loop. The output will be:

hello world! this is a test string.