Skip to content

Instantly share code, notes, and snippets.

@tristanwietsma
Last active December 16, 2015 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tristanwietsma/5477937 to your computer and use it in GitHub Desktop.
Save tristanwietsma/5477937 to your computer and use it in GitHub Desktop.
Go file I/O example This shows how to read a text file in Go, line-by-line. The file is supplied via command-line. A list of lines is built using a slice and the append function. Duplicate lines are prevented using a map. The resulting list of lines is sorted and displayed.
Aardvark
Elephant
Albatross
Alligator
Alpaca
Anaconda
Ant
Anteater
Antelope
Armadillo
Puffin
Horse
Baboon
Badger
Eagle
Bandicoot
Tiger
Barnacle
Barracuda
Basilisk
Bass
Hound
Bat
Dragon
Beaver
Bee
Beetle
Whale
Sheep
Bison
Goat
Bear
Fly
Rhino
Spider
Blackbird
Blowfish
Boa
Boar
Bonobo
Dolphin
Boxer
Buck
Budgie
Buffalo
Frog
Butterfly
Buzzard
Caribou
Cheetah
Seal
Caiman
Camel
Canary
Carp
Cat
Caterpillar
Cattle
Catfish
Chameleon
Centipede
Chicken
Chimpanzee
Chihuahua
Chinchilla
Chipmunk
Clam
Chupacabra
Cobra
Cockatiel
Cockatoo
Cockroach
Cod
Coho
Snake
Cougar
Cow
Coyote
Crab
Crane
Crawfish
Cricket
Crocodile
Crow
Deer
Dog
Degus
Dingo
Dinosaur
Donkey
Dove
Dragonfly
Drake
Duck
Platypus
Lynx
Eagle
Echidna
Eclectus
Eel
Egret
Elk
Emu
Erne
Falcon
Finch
Fish
Firefly
Flamingo
Flatworm
Fly
Ferret
Fox
Frog
Iguana
Tortoise
Gazelle
Panda
Giraffe
Goat
Goose
Gopher
Gorilla
Grasshopper
Turtle
Groundhog
Hare
Hawk
Hedgehog
Heron
Herring
Hippopotamus
Horse
Hyena
Hyrax
Iguana
Impala
Jellyfish
Jackal
Jackrabbit
Jaguar
Kangaroo
Cobra
Kingfisher
Koala
Kookaburra
Krill
Lama
Lamb
Lancelet
Leech
Lemming
Lemur
Leopard
Lice
Lion
Lionfish
Llama
Lobster
Lynx
Manatee
Mantis
Marmot
Marsupials
Meerkat
Mink
Mole
Mollusks
Mongoose
Monkey
Moose
Mouse
Mule
Muskox
Muskrat
Narwhal
Nautilus
Nene
Newt
Nutria
Nyala
Ocelot
Octopus
Okapi
Opossum
Orangutan
Orca
Osprey
Ostrich
Otter
Owl
Ox
Panda
Panther
Peacock
Pelican
Penguin
Pig
Pigeon
Platypus
Potto
Porcupine
Mantis
Prawn
Puma
Quail
Quetzal
Rabbit
Raccoon
Rat
Ray
Reindeer
Rhino
Rhinoceros
Robin
Rooster
Roundworm
Salmon
Crocodile
Sandpiper
Seahorse
Seal
Anemone
Urchin
Scallop
Scorpion
Shark
Sheep
Shrimp
Sloth
Slugs
Snails
Snake
Hare
Fox
Leopard
Sponge
Squid
Squirrel
Starfish
Stork
Swan
Swordfish
Tapir
Tiger
Tadpole
Tamarin
Tapeworm
Tarantula
Tarpan
Terrapin
Tick
Tortoise
Trout
Turkey
Turtle
Uakari
Urchin
Urutu
Vicuna
Viper
Vulture
Raptor
Vole
Vervet
Walrus
Warbler
Warthog
Wasp
Buffalo
Wallaby
Whale
Whippet
Whooper
Weasel
Weevil
Wolf
Wolverine
Woodchuck
Woodpecker
Wren
Wombat
Wildcat
Wildebeest
Xantus
Xenop
Yak
Yeti
Zander
Zebra
Zorilla
Zebu
package main
import ("bufio"; "os"; "io"; "fmt"; "sort"; "strings")
func ReadLines(filename string) []string {
file, _ := os.Open(filename)
r := bufio.NewReader(file)
lines := []string{}
m := make(map[string]int)
var txt string
for {
line, _, err := r.ReadLine()
if err == io.EOF {
break
}
txt = strings.Trim(string(line), " ")
_, ok := m[txt]
if !ok {
lines = append(lines, txt)
m[txt] = 1
}
}
sort.Strings(lines)
return lines
}
func main() {
x := ReadLines(os.Args[1])
for i := 0; i < len(x); i++ {
fmt.Printf("%v\t%s\n", i+1, x[i])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment