Skip to content

Instantly share code, notes, and snippets.

@me7
Created April 23, 2015 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save me7/45ee299a91741782ebe2 to your computer and use it in GitHub Desktop.
Save me7/45ee299a91741782ebe2 to your computer and use it in GitHub Desktop.
walk directory and read file content to array of strings
package main
import (
"fmt"
"path/filepath"
"os"
"io/ioutil"
"strings"
)
func walkFn(path string, info os.FileInfo, err error) error {
filename := info.Name()
if strings.Contains(filename, "csv") {
basename := filepath.Base(filename)
println(strings.TrimSuffix(basename, filepath.Ext(basename)))
content, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
lines := strings.Split(string(content), "\r\n")
fmt.Printf("%v\n\n",lines[7])
}
return nil
}
func main(){
fmt.Println("start run")
err := filepath.Walk(".", walkFn)
if err != nil {
println(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment