Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
Metadata
ID:go-best-practices/bytes-splitn
Language: Go
Severity: Warning
Category: Best Practices
Description
In Go, the bytes.SplitN() and bytes.SplitAfterN() functions are used to split a byte slice into multiple byte slices based on a delimiter. The functions take in two parameters: the original byte slice to be split and the delimiter byte slice. Additionally, they can take an optional integer limit parameter, which determines the number of byte slices to be returned.
It is recommended to avoid invoking bytes.SplitN() or bytes.SplitAfterN() with a negative limit, as it can lead to unexpected behavior and potential errors in your code. If the limit is negative, there is no limit on the number of byte slices returned. This means that the original byte slice will be split into as many slices as possible, resulting in an unpredictable number of slices.
In this example, bytes.SplitN() will split the byte slice "Hello,World,Welcome" at every occurrence of the delimiter "," and return all byte slices. The resulting slices will be [Hello World Welcome].
To avoid using a negative limit with bytes.SplitN() or bytes.SplitAfterN(), it is recommended to use a positive limit or to use the bytes.Split() or bytes.SplitAfter() functions.
Using a positive limit allows you to control the maximum number of byte slices returned.
In this case, bytes.SplitN() will split the byte slice "Hello,World,Welcome" at the first occurrence of the delimiter "," and return two slices: [Hello World,Welcome].
Alternatively, you can use the bytes.Split() or bytes.SplitAfter() functions without specifying a limit. These functions split the byte slice into all possible slices based on the delimiter.