Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]kadai3 happylifetaka #44

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions kadai3-1/happylifetaka/typing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"os"

"github.com/happylifetaka/dojo4/kadai3-1/happylifetaka/typinggame"
)

func main() {
var t typinggame.TypingGame
t.Start(os.Stdin)
}
82 changes: 82 additions & 0 deletions kadai3-1/happylifetaka/typinggame/typinggame.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package typinggame

import (
"bufio"
"fmt"
"io"
"math/rand"
"time"
)

//TypingGame 1 minute typing game.
type TypingGame int

//Start start 1 minute typing game.
//Param
//r:io.Reader example:os.Stdin
func (t *TypingGame) Start(r io.Reader) {
ch1 := input(r)
ch2 := wait(60)
var words = []string{"apple", "banana", "cherry", "plum", "grape", "pineapple"}
happylifetaka marked this conversation as resolved.
Show resolved Hide resolved

shuffle(words)
i := 0
//success count
sucessCnt := 0
//fail count
failCnt := 0
fmt.Println("try typing.1 minute.")
fmt.Println(words[i])

TIMEOUT_LABEL:
for {
select {
case msg := <-ch1:
if words[i] == msg {
if len(words) <= (i + 1) {
i = 0
} else {
i++
}
sucessCnt++
} else {
fmt.Println("miss.retyping words.")
failCnt++
}
fmt.Println(words[i])
case <-ch2:
fmt.Println("")
fmt.Println("time up.success count:", sucessCnt, " fail count:", failCnt)

break TIMEOUT_LABEL
}
}
}

func shuffle(a []string) {
for i := len(a) - 1; i >= 0; i-- {
j := rand.Intn(i + 1)
a[i], a[j] = a[j], a[i]
}
}

func input(r io.Reader) <-chan string {
ch := make(chan string)
go func() {
s := bufio.NewScanner(r)
defer close(ch)
for s.Scan() {
ch <- s.Text()
}
}()
return ch
}

func wait(sec int) <-chan bool {
ch := make(chan bool)
go func() {
time.Sleep(time.Duration(sec) * time.Second)
ch <- true
}()
return ch
}
41 changes: 41 additions & 0 deletions kadai3-2/happylifetaka/downloader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"flag"
"fmt"
"os"

"github.com/happylifetaka/dojo4/kadai3-2/happylifetaka/downloader"
)

type downloadByteInfo struct {
start int64
end int64
}

func main() {
div := flag.Int64("div", 3, "file download division")
usageMsg := "udage:downloader [-div] url saveFilePath"
flag.Usage = func() {
fmt.Println(usageMsg)
flag.PrintDefaults()
os.Exit(0)
}
flag.Parse()
args := flag.Args()
if len(args) != 2 {
fmt.Println("parameter error.")
fmt.Println(usageMsg)
flag.PrintDefaults()
os.Exit(0)
}
url := args[0]
saveFilePath := args[1]

var d downloader.Downloader
if err := d.Download(url, saveFilePath, *div); err != nil {
fmt.Println(err)
os.Exit(1)
}
os.Exit(0)
}
157 changes: 157 additions & 0 deletions kadai3-2/happylifetaka/downloader/downloader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package downloader

import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"

"golang.org/x/sync/errgroup"
)

type Downloader int

type downloadInfo struct {
startByte int64
endByte int64
}

// Download
// description:Download specified URL.
// parameter
// url :download target url.
// saveFilePath:save file path.
// div:Number of divided downloads.
func (d *Downloader) Download(url string, saveFilePath string, div int64) error {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

連続する変数の型が同じ場合は省略可能です (url, saveFilePath string, div int64)

res, err := http.Head(url)
if err != nil {
fmt.Println("http Head error")
return err
}
if res.StatusCode != 200 {
fmt.Println("bad status code")
fmt.Println("status code:", res.StatusCode)
return err
}

if !canRangeDownload(res.Header) {
fmt.Println("range download not support.")
return err
}

di := splitDownloadLength(res.ContentLength, div)
filenames := make([]string, div)

var eg errgroup.Group

i := 1
for _, d := range di {
j := i
eg.Go(func() error {
err := rangeDownload(j, url, d.startByte, d.endByte)
return err
})
filenames[i-1] = strconv.Itoa(j) + ".temp.download"
i++
}
if err := eg.Wait(); err != nil {
fmt.Println("download error")
return err
}

if err := joinFiles(filenames, saveFilePath); err != nil {
fmt.Println("join file error")
return err
}

if err := deleteFiles(filenames); err != nil {
fmt.Println("join file error")
return err
}
fmt.Println("download finish.")
return nil
}

func canRangeDownload(h http.Header) bool {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http.Header には Get(key string) (value string) が実装されているので、forせずに確認することもできます

f := false
for k, v := range h {
if k == "Accept-Ranges" && len(v) > 0 && v[0] == "bytes" {
f = true
break
}
}
return f
}

func splitDownloadLength(length int64, div int64) []downloadInfo {

divLength := length / div

a := make([]downloadInfo, div)

var i int64
for i = 0; i < div; i++ {
s := i * divLength
e := (i+1)*divLength - 1

a[i] = downloadInfo{s, e}
}
return a
}

func rangeDownload(index int, url string, s int64, e int64) error {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}

req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", s, e))

res, err := http.DefaultClient.Do(req)
defer res.Body.Close()
if err != nil {
return err
}

out_path := strconv.Itoa(index) + ".temp.download"
out, err := os.Create(out_path)
defer out.Close()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

エラーのチェックをしてから out の変数を触ったほうが良いです
理由は、エラーが帰ってきている時点でファイルが作成されているかどうかわからない( out がnilの可能性がある)ため、そのCloseは利用しないほうが良いです

if err != nil {
return err
}

io.Copy(out, res.Body)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

io.Copyもエラーを返す可能性があるので、捨てないほうが良いです

return nil
}

func joinFiles(filenames []string, saveFilePath string) error {
files := make([]io.Reader, len(filenames))

for i, filename := range filenames {
files[i], _ = os.Open(filename)
}

reader := io.MultiReader(files...)
b, _ := ioutil.ReadAll(reader)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

エラーは捨てないほうが良いです


file, err := os.Create(saveFilePath)
if err != nil {
return err
}
defer file.Close()

file.Write(([]byte)(b))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ioutil.ReadAllですべて読み込んでしまって、Writeするよりもio.Copyで扱った方が効率が良い場合が多いです

return nil
}

func deleteFiles(filenames []string) error {
for _, f := range filenames {
err := os.Remove(f)
if err != nil {
return err
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

場合によりますが、ここにおけるエラーは一つのファイルを消せないことでなく、消せなかったファイルが存在することだと思います
したがって、途中でエラーになった場合でもそのエラーを保存し、最後まで実行して、消せなかったファイル一覧としたほうが良いと思います

}
}
return nil
}