move error check out of the for loop

The `bufio.Scanner.Scan` method returns false either by reaching the
end of the input or an error. After Scan returns false, the Err method
will return any error that occurred during scanning, except that if it
was io.EOF, Err will return nil.

We should check the error when Scan return false(out of the for loop).

Signed-off-by: Wang Long <long.wanglong@huawei.com>
This commit is contained in:
Wang Long 2017-01-17 21:25:00 +08:00
parent a9610f2c02
commit 3a71eb0256
2 changed files with 11 additions and 11 deletions

View File

@ -189,9 +189,6 @@ func GetAllSubsystems() ([]string, error) {
s := bufio.NewScanner(f) s := bufio.NewScanner(f)
for s.Scan() { for s.Scan() {
if err := s.Err(); err != nil {
return nil, err
}
text := s.Text() text := s.Text()
if text[0] != '#' { if text[0] != '#' {
parts := strings.Fields(text) parts := strings.Fields(text)
@ -200,6 +197,9 @@ func GetAllSubsystems() ([]string, error) {
} }
} }
} }
if err := s.Err(); err != nil {
return nil, err
}
return subsystems, nil return subsystems, nil
} }
@ -265,10 +265,6 @@ func parseCgroupFromReader(r io.Reader) (map[string]string, error) {
cgroups := make(map[string]string) cgroups := make(map[string]string)
for s.Scan() { for s.Scan() {
if err := s.Err(); err != nil {
return nil, err
}
text := s.Text() text := s.Text()
// from cgroups(7): // from cgroups(7):
// /proc/[pid]/cgroup // /proc/[pid]/cgroup
@ -285,6 +281,10 @@ func parseCgroupFromReader(r io.Reader) (map[string]string, error) {
cgroups[subs] = parts[2] cgroups[subs] = parts[2]
} }
} }
if err := s.Err(); err != nil {
return nil, err
}
return cgroups, nil return cgroups, nil
} }

View File

@ -212,10 +212,6 @@ func parseStatusFile(path string) (map[string]string, error) {
status := make(map[string]string) status := make(map[string]string)
for s.Scan() { for s.Scan() {
if err := s.Err(); err != nil {
return nil, err
}
text := s.Text() text := s.Text()
parts := strings.Split(text, ":") parts := strings.Split(text, ":")
@ -225,5 +221,9 @@ func parseStatusFile(path string) (map[string]string, error) {
status[parts[0]] = parts[1] status[parts[0]] = parts[1]
} }
if err := s.Err(); err != nil {
return nil, err
}
return status, nil return status, nil
} }