{"id":292,"date":"2019-10-19T04:06:13","date_gmt":"2019-10-19T04:06:13","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=292"},"modified":"2019-11-12T19:36:03","modified_gmt":"2019-11-12T19:36:03","slug":"read-large-file-word-by-word-go","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/read-large-file-word-by-word-go\/","title":{"rendered":"Read a large file Word by Word in Go(Golang)"},"content":{"rendered":"\n

When it comes to reading large files, obviously we don’t want to load the entire file in memory. bufio package in golang comes to the rescue when reading large files in Golang. Let’s say we have a sample.txt file with below contents<\/p>\n\n\n\n

This is an example\nto show how\nto read file\nword by word.<\/code><\/pre>\n\n\n\n

Word by Word scan<\/strong><\/h2>\n\n\n\n
package main\nimport (\n    \"bufio\"\n    \"fmt\"\n    \"log\"\n    \"os\"\n)\nfunc main() {\n    WordbyWordScan()\n}\nfunc WordbyWordScan() {\n    file, err := os.Open(\".\/scanner\/sample.txt\")\n    if err != nil {\n        log.Fatal(err)\n    }\n    defer file.Close()\n    scanner := bufio.NewScanner(file)\n    scanner.Split(bufio.ScanWords)\n    for scanner.Scan() {\n        fmt.Println(scanner.Text())\n    }\n    if err := scanner.Err(); err != nil {\n        log.Fatal(err)\n    }\n}<\/code><\/pre>\n\n\n\n

Output:<\/strong><\/p>\n\n\n\n

This\nis\nan\nexample\nto\nshow\nhow\nto\nread\nfile\nline\nby\nline\nand\nword\nby\nword.<\/code><\/pre>\n\n\n\n

Please note that in the above program we set scanner.Split(bufio.ScanWords)<\/strong> which helps us to read the file word by word. Note however bufio.Scanner has max buffer size 64*1024 bytes which means in case you file has any line greater than the size of 64*1024, then it will give the error <\/p>\n\n\n\n

bufio.Scanner: token too long<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

When it comes to reading large files, obviously we don’t want to load the entire file in memory. bufio package in golang comes to the rescue when reading large files in Golang….<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"class_list":["post-292","post","type-post","status-publish","format-standard","hentry","category-tech"],"yoast_head":"\nRead a large file Word by Word in Go(Golang) - Welcome To Golang By Example<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/golangbyexamples.com\/read-large-file-word-by-word-go\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Read a large file Word by Word in Go(Golang) - Welcome To Golang By Example\" \/>\n<meta property=\"og:description\" content=\"When it comes to reading large files, obviously we don’t want to load the entire file in memory. bufio package in golang comes to the rescue when reading large files in Golang....\" \/>\n<meta property=\"og:url\" content=\"https:\/\/golangbyexamples.com\/read-large-file-word-by-word-go\/\" \/>\n<meta property=\"og:site_name\" content=\"Welcome To Golang By Example\" \/>\n<meta property=\"article:published_time\" content=\"2019-10-19T04:06:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-11-12T19:36:03+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/golangbyexamples.com\/read-large-file-word-by-word-go\/\",\"url\":\"https:\/\/golangbyexamples.com\/read-large-file-word-by-word-go\/\",\"name\":\"Read a large file Word by Word in Go(Golang) - Welcome To Golang By Example\",\"isPartOf\":{\"@id\":\"https:\/\/golangbyexamples.com\/#website\"},\"datePublished\":\"2019-10-19T04:06:13+00:00\",\"dateModified\":\"2019-11-12T19:36:03+00:00\",\"author\":{\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f\"},\"breadcrumb\":{\"@id\":\"https:\/\/golangbyexamples.com\/read-large-file-word-by-word-go\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/golangbyexamples.com\/read-large-file-word-by-word-go\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/golangbyexamples.com\/read-large-file-word-by-word-go\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/golangbyexamples.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Read a large file Word by Word in Go(Golang)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/golangbyexamples.com\/#website\",\"url\":\"https:\/\/golangbyexamples.com\/\",\"name\":\"Welcome To Golang By Example\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/golangbyexamples.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"url\":\"https:\/\/golangbyexamples.com\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Read a large file Word by Word in Go(Golang) - Welcome To Golang By Example","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/golangbyexamples.com\/read-large-file-word-by-word-go\/","og_locale":"en_US","og_type":"article","og_title":"Read a large file Word by Word in Go(Golang) - Welcome To Golang By Example","og_description":"When it comes to reading large files, obviously we don’t want to load the entire file in memory. bufio package in golang comes to the rescue when reading large files in Golang....","og_url":"https:\/\/golangbyexamples.com\/read-large-file-word-by-word-go\/","og_site_name":"Welcome To Golang By Example","article_published_time":"2019-10-19T04:06:13+00:00","article_modified_time":"2019-11-12T19:36:03+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/golangbyexamples.com\/read-large-file-word-by-word-go\/","url":"https:\/\/golangbyexamples.com\/read-large-file-word-by-word-go\/","name":"Read a large file Word by Word in Go(Golang) - Welcome To Golang By Example","isPartOf":{"@id":"https:\/\/golangbyexamples.com\/#website"},"datePublished":"2019-10-19T04:06:13+00:00","dateModified":"2019-11-12T19:36:03+00:00","author":{"@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f"},"breadcrumb":{"@id":"https:\/\/golangbyexamples.com\/read-large-file-word-by-word-go\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/golangbyexamples.com\/read-large-file-word-by-word-go\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/golangbyexamples.com\/read-large-file-word-by-word-go\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/golangbyexamples.com\/"},{"@type":"ListItem","position":2,"name":"Read a large file Word by Word in Go(Golang)"}]},{"@type":"WebSite","@id":"https:\/\/golangbyexamples.com\/#website","url":"https:\/\/golangbyexamples.com\/","name":"Welcome To Golang By Example","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/golangbyexamples.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","caption":"admin"},"url":"https:\/\/golangbyexamples.com\/author\/admin\/"}]}},"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paOs1b-4I","amp_validity":null,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/292","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/comments?post=292"}],"version-history":[{"count":3,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/292\/revisions"}],"predecessor-version":[{"id":495,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/292\/revisions\/495"}],"wp:attachment":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/media?parent=292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/categories?post=292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/tags?post=292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}