Created
April 21, 2026 16:16
-
-
Save possebon/aebb6c05c36da37e3ad8a3fa17c67569 to your computer and use it in GitHub Desktop.
SQL Server GO batch splitter in Go — line-by-line, case-insensitive, dodges the 'GOPHER' trap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // split-go-batches.go — line-by-line GO batch splitter for SQL Server clients. | |
| // | |
| // GO is not a SQL Server keyword. It's a client convention. If you write your | |
| // own SQL Server tooling (like I did with a Go-based DBA diagnostic agent), | |
| // you need to split on GO before sending each batch over TDS. The naive regex | |
| // on substring "GO" breaks on strings like 'GOPHER'. This version matches GO | |
| // only when it's on its own line, case-insensitive, with whitespace trimmed. | |
| // | |
| // Written up in: https://www.linkedin.com/in/fernando-possebon | |
| // | |
| // Test cases that justify the exact-match approach: | |
| // - Input containing `SELECT 'GOPHER' AS animal` — must NOT split | |
| // - Input with `go`, `Go`, and `GO` on their own lines — must all split | |
| // - Input with trailing `GO ` (trailing whitespace) — must split | |
| package main | |
| import "strings" | |
| func splitGoBatches(input string) []string { | |
| lines := strings.Split(input, "\n") | |
| var batches []string | |
| var current []string | |
| for _, line := range lines { | |
| trimmed := strings.TrimSpace(line) | |
| if strings.EqualFold(trimmed, "GO") { | |
| batch := strings.TrimSpace(strings.Join(current, "\n")) | |
| if batch != "" { | |
| batches = append(batches, batch) | |
| } | |
| current = nil | |
| continue | |
| } | |
| current = append(current, line) | |
| } | |
| if last := strings.TrimSpace(strings.Join(current, "\n")); last != "" { | |
| batches = append(batches, last) | |
| } | |
| return batches | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment