Go Text File Read to byte array

Description

Go Text File Read to byte array

package main /*from   www  .  j a  v  a  2  s .c o m*/

import ( 
    "fmt" 
    "os" 
) 

func main() { 
    file, err  := os.Open("main.go") 
    if err  != nil { 
        // handle the error here 
        return 
    } 
    defer file.Close() 

    // get the file size 
    stat, err  := file.Stat() 
    if err  != nil { 
        return 
    } 
    // read the file 
    bs  := make([]byte, stat.Size()) 
    _, err = file.Read(bs) 
    if err  != nil { 
        return 
    } 

    str  := string(bs) 
    fmt.Println(str) 
} 



PreviousNext

Related