FAQ
This script:

[code]
package main

import (
     "fmt"
     "os"
     "path/filepath"
     "flag"
     "crypto/sha256"
     "log"
     "encoding/hex"
)

var pattern string //pattern search expression
var funcio = sha256.New()

func VisitFile(fp string, fi os.FileInfo, err error) error {
     if err != nil {
         fmt.Println(err) // can't walk here,
         return nil // but continue walking elsewhere
     }
     if !!fi.IsDir() {
         return nil // not a file. ignore.
     }
     matched, err := filepath.Match(pattern, fi.Name())
     if err != nil {
         fmt.Println(err) // malformed pattern
         return err // this is fatal.
     }
     if matched {
funcio.Reset()
file, err := os.Open(fp) // For read access.
if err != nil {
log.Fatal(err)
}
data := make([]byte, 100)
count, error := file.Read(data)
if error != nil {
log.Fatal(error)
}
defer file.Close()
funcio.Write(data)
         fmt.Println(fp, hex.EncodeToString(funcio.Sum(nil)), count)
     }
     return nil
}

func main() {
     ruta := flag.String("path", "./", "Source of files")
     pattern = *flag.String("pattern", "*", "Pattern search expression of
searching files")

     flag.Parse()
     fmt.Println("Patró:", pattern, "Ruta:", ruta)
     filepath.Walk(*ruta, VisitFile)
}
[/code]

don't assign properly 'pattern' variable (it's always equal to '*'). Why?
Anyone could help me?

Thanks,
Xan.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Matthew Kane at Jun 25, 2013 at 4:26 pm
    If you want the flag package to store something in a package-level
    variable, try using the StringVar function.

    flag.StringVar(&pattern. "pattern", "*", "Pattern search expression of
    searching files")

    What you are doing is dereferencing the pointer and copying who-knows-what
    out of some location into your pattern variable before calling flag.Parse().

    On Tue, Jun 25, 2013 at 12:18 PM, Xan wrote:

    This script:

    [code]
    package main

    import (
    "fmt"
    "os"
    "path/filepath"
    "flag"
    "crypto/sha256"
    "log"
    "encoding/hex"
    )

    var pattern string //pattern search expression
    var funcio = sha256.New()

    func VisitFile(fp string, fi os.FileInfo, err error) error {
    if err != nil {
    fmt.Println(err) // can't walk here,
    return nil // but continue walking elsewhere
    }
    if !!fi.IsDir() {
    return nil // not a file. ignore.
    }
    matched, err := filepath.Match(pattern, fi.Name())
    if err != nil {
    fmt.Println(err) // malformed pattern
    return err // this is fatal.
    }
    if matched {
    funcio.Reset()
    file, err := os.Open(fp) // For read access.
    if err != nil {
    log.Fatal(err)
    }
    data := make([]byte, 100)
    count, error := file.Read(data)
    if error != nil {
    log.Fatal(error)
    }
    defer file.Close()
    funcio.Write(data)
    fmt.Println(fp, hex.EncodeToString(funcio.Sum(nil)), count)
    }
    return nil
    }

    func main() {
    ruta := flag.String("path", "./", "Source of files")
    pattern = *flag.String("pattern", "*", "Pattern search expression of
    searching files")

    flag.Parse()
    fmt.Println("Patró:", pattern, "Ruta:", ruta)
    filepath.Walk(*ruta, VisitFile)
    }
    [/code]

    don't assign properly 'pattern' variable (it's always equal to '*'). Why?
    Anyone could help me?

    Thanks,
    Xan.

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.



    --
    matt kane
    twitter: the_real_mkb / nynexrepublic
    http://hydrogenproject.com

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Xancorreu at Jun 25, 2013 at 6:00 pm
    Yes:
    flag.StringVar(&pattern, "pattern", "*", "Pattern search expression of
    searching files")

    Thanks,
    Xan.

    Al 25/06/13 18:25, En/na Matthew Kane ha escrit:
    If you want the flag package to store something in a package-level
    variable, try using the StringVar function.

    flag.StringVar(&pattern. "pattern", "*", "Pattern search expression of
    searching files")

    What you are doing is dereferencing the pointer and copying
    who-knows-what out of some location into your pattern variable before
    calling flag.Parse().


    On Tue, Jun 25, 2013 at 12:18 PM, Xan wrote:

    This script:

    [code]
    package main

    import (
    "fmt"
    "os"
    "path/filepath"
    "flag"
    "crypto/sha256"
    "log"
    "encoding/hex"
    )

    var pattern string //pattern search expression
    var funcio = sha256.New()

    func VisitFile(fp string, fi os.FileInfo, err error) error {
    if err != nil {
    fmt.Println(err) // can't walk here,
    return nil // but continue walking elsewhere
    }
    if !!fi.IsDir() {
    return nil // not a file. ignore.
    }
    matched, err := filepath.Match(pattern, fi.Name())
    if err != nil {
    fmt.Println(err) // malformed pattern
    return err // this is fatal.
    }
    if matched {
    funcio.Reset()
    file, err := os.Open(fp) // For read access.
    if err != nil {
    log.Fatal(err)
    }
    data := make([]byte, 100)
    count, error := file.Read(data)
    if error != nil {
    log.Fatal(error)
    }
    defer file.Close()
    funcio.Write(data)
    fmt.Println(fp, hex.EncodeToString(funcio.Sum(nil)), count)
    }
    return nil
    }

    func main() {
    ruta := flag.String("path", "./", "Source of files")
    pattern = *flag.String("pattern", "*", "Pattern search
    expression of searching files")

    flag.Parse()
    fmt.Println("Patró:", pattern, "Ruta:", ruta)
    filepath.Walk(*ruta, VisitFile)
    }
    [/code]

    don't assign properly 'pattern' variable (it's always equal to
    '*'). Why? Anyone could help me?

    Thanks,
    Xan.
    --
    You received this message because you are subscribed to the Google
    Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it,
    send an email to golang-nuts+unsubscribe@googlegroups.com
    For more options, visit https://groups.google.com/groups/opt_out.





    --
    matt kane
    twitter: the_real_mkb / nynexrepublic
    http://hydrogenproject.com
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 25, '13 at 4:18p
activeJun 25, '13 at 6:00p
posts3
users2
websitegolang.org

2 users in discussion

Xancorreu: 2 posts Matthew Kane: 1 post

People

Translate

site design / logo © 2021 Grokbase