Created
June 16, 2016 06:42
-
-
Save JesseEisen/aec959e6ccf7da486f45fbd19b2bc9e2 to your computer and use it in GitHub Desktop.
Go version tail implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"os" | |
"strconv" | |
"strings" | |
) | |
var n int | |
var file_name string | |
var err error | |
func tail(s []string) { | |
array_len := len(s) - 1 | |
if array_len <= n { | |
for i := 0; i < array_len; i++ { | |
fmt.Printf("%s\n", s[i]) | |
} | |
} else if array_len > n { | |
for i := array_len - n; i < array_len; i++ { | |
fmt.Printf("%s\n", s[i]) | |
} | |
} | |
} | |
func main() { | |
if len(os.Args) == 1 { | |
n = 10 | |
file_name = "hello.txt" | |
} else if len(os.Args) == 2 { | |
n, err = strconv.Atoi(os.Args[1]) | |
if err != nil { | |
fmt.Println("can not convert") | |
} | |
file_name = "hello.txt" | |
} else if len(os.Args) == 3 { | |
file_name = os.Args[2] | |
n, err = strconv.Atoi(os.Args[1]) | |
if err != nil { | |
fmt.Println("can not convert") | |
} | |
} | |
f, err := ioutil.ReadFile(file_name) | |
if err != nil { | |
log.Fatal(err) | |
} | |
s := strings.Split(string(f), "\n") | |
tail(s) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment