How to Split Large Files on Windows with a Simple Go Tool
This guide explains how to create a cross‑platform Go command‑line utility that efficiently splits multi‑gigabyte files on Windows into user‑defined chunk sizes, covering the rationale for using Go, core implementation steps, full source code, and practical usage instructions.
Background
Large files such as logs or database backups can be several gigabytes in size, making them slow to open and difficult to process with standard Windows text editors.
Why Go?
Cross‑platform compilation : a single source builds native binaries for Windows, Linux and macOS.
Rich standard library : built‑in support for file I/O, path handling and command‑line flags.
Efficient binaries : small executable size and good performance for heavy I/O workloads.
Implementation Overview
Open the source file.
Read data in chunks of the size specified by the user.
Write each chunk to a new part file named originalname.N, where N is the part number.
Repeat until the entire source file has been processed.
Source Code
package main
import (
"flag"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
)
func splitFile(filePath string, chunkSize int64) error {
file, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("cannot open file: %v", err)
}
defer file.Close()
fileInfo, err := file.Stat()
if err != nil {
return fmt.Errorf("cannot stat file: %v", err)
}
_ = fileInfo // size not needed directly
fileName := filepath.Base(filePath)
dir := filepath.Dir(filePath)
buffer := make([]byte, 1024) // 1 KB buffer
partNumber := 1
for {
partFileName := filepath.Join(dir, fmt.Sprintf("%s.%d", fileName, partNumber))
partFile, err := os.Create(partFileName)
if err != nil {
return fmt.Errorf("cannot create part file: %v", err)
}
var written int64 = 0
for written < chunkSize {
n, err := file.Read(buffer)
if err != nil && err != io.EOF {
partFile.Close()
return fmt.Errorf("read error: %v", err)
}
if n == 0 {
break // EOF
}
nw, err := partFile.Write(buffer[:n])
if err != nil {
partFile.Close()
return fmt.Errorf("write error: %v", err)
}
written += int64(nw)
}
partFile.Close()
fmt.Printf("created part: %s
", partFileName)
if written < chunkSize {
break // finished reading source file
}
partNumber++
}
fmt.Println("splitting completed")
return nil
}
func main() {
filePath := flag.String("file", "", "path of the file to split")
chunkSizeMB := flag.Int64("size", 1, "size of each part in megabytes")
flag.Parse()
if *filePath == "" {
fmt.Println("file path is required")
flag.Usage()
return
}
chunkSize := *chunkSizeMB * 1024 * 1024 // convert MB to bytes
if err := splitFile(*filePath, chunkSize); err != nil {
fmt.Printf("error: %v
", err)
}
}Code Explanation
Command‑line flags : the flag package defines -file for the source path and -size for the desired part size (default 1 MB).
Chunked I/O : a 1 KB buffer is reused; data is read from the source file until the accumulated bytes reach the requested chunk size, then the part file is closed.
Error handling : any failure while opening, reading, writing or creating files is reported and aborts the process.
Usage
Compile : go build split_file.go Run : ./split_file -file "C:\path\to\largefile.txt" -size 50 creates 50 MB parts named largefile.txt.1, largefile.txt.2, …
Conclusion
The Go‑based splitter provides a lightweight, cross‑platform way to break multi‑gigabyte files into manageable chunks on Windows (and other OSes). Its simple design makes it easy to extend with features such as merging, custom naming schemes, or additional metadata.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ops Development & AI Practice
DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
