refactor: file append exercises
This commit is contained in:
@ -1,7 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
// EXERCISE: Sort to a file
|
// EXERCISE: Sort and write items to a file
|
||||||
//
|
//
|
||||||
// 1. Get arguments from command-line
|
// 1. Get arguments from command-line
|
||||||
//
|
//
|
||||||
@ -25,17 +25,18 @@ package main
|
|||||||
//
|
//
|
||||||
// HINTS
|
// HINTS
|
||||||
//
|
//
|
||||||
// ONLY READ THIS IF YOU GET STUCK
|
// + REMEMBER: os.Args is a []string
|
||||||
//
|
//
|
||||||
// Below, []string means string slice, []byte means byte slice.
|
// + String slices are sortable using `sort.Strings`
|
||||||
//
|
//
|
||||||
// + You can use the os.Args[1:] to get a []string
|
|
||||||
// + Then you can sort it using sort.Strings
|
|
||||||
// + Use ioutil.WriteFile to write to a file.
|
// + Use ioutil.WriteFile to write to a file.
|
||||||
|
//
|
||||||
// + But you need to convert []string to []byte to be able to
|
// + But you need to convert []string to []byte to be able to
|
||||||
// write it to a file using the ioutil.WriteFile.
|
// write it to a file using the ioutil.WriteFile.
|
||||||
|
//
|
||||||
// + To do that, create a new []byte and append the elements of your
|
// + To do that, create a new []byte and append the elements of your
|
||||||
// []string.
|
// []string.
|
||||||
|
//
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -8,9 +8,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
// EXERCISE: Sort to a file with ordinals
|
// EXERCISE: Sort and write items to a file with their ordinals
|
||||||
//
|
//
|
||||||
// Use the previous exercise: Append #4
|
// Use the previous exercise.
|
||||||
//
|
//
|
||||||
// This time, print the sorted items with ordinals
|
// This time, print the sorted items with ordinals
|
||||||
// (see the expected output)
|
// (see the expected output)
|
||||||
@ -35,10 +35,10 @@ import (
|
|||||||
//
|
//
|
||||||
// + You can use strconv.AppendInt function to append an int
|
// + You can use strconv.AppendInt function to append an int
|
||||||
// to a byte slice. strconv contains a lot of functions for appending
|
// to a byte slice. strconv contains a lot of functions for appending
|
||||||
// other basic types as well.
|
// other basic types to []byte slices as well.
|
||||||
//
|
//
|
||||||
// + You can append individual characters to a byte slice using
|
// + You can append individual characters to a byte slice using
|
||||||
// rune literals:
|
// rune literals (because: rune literal are typeless numerics):
|
||||||
//
|
//
|
||||||
// var slice []byte
|
// var slice []byte
|
||||||
// slice = append(slice, 'h', 'i', ' ', '!')
|
// slice = append(slice, 'h', 'i', ' ', '!')
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
// EXERCISE: Print the directories
|
// EXERCISE: Find and write the names of subdirectories to a file
|
||||||
//
|
//
|
||||||
// Create a program that can get multiple directory paths from
|
// Create a program that can get multiple directory paths from
|
||||||
// the command-line, and prints only their subdirectories into a
|
// the command-line, and prints only their subdirectories into a
|
||||||
@ -43,7 +43,14 @@ package main
|
|||||||
// (A directory is also a file)
|
// (A directory is also a file)
|
||||||
//
|
//
|
||||||
// + You can use IsDir method of a FileInfo value to detect
|
// + You can use IsDir method of a FileInfo value to detect
|
||||||
// whether a file is a directory or not: go doc os.FileInfo.IsDir
|
// whether a file is a directory or not.
|
||||||
|
//
|
||||||
|
// Check out its documentation:
|
||||||
|
//
|
||||||
|
// go doc os.FileInfo.IsDir
|
||||||
|
//
|
||||||
|
// # or using godocc
|
||||||
|
// godocc os.FileInfo.IsDir
|
||||||
//
|
//
|
||||||
// + You can use '\t' escape sequence for indenting the subdirs.
|
// + You can use '\t' escape sequence for indenting the subdirs.
|
||||||
//
|
//
|
||||||
|
Reference in New Issue
Block a user