restructure: arrays and slices

This commit is contained in:
Inanc Gumus
2019-01-29 19:43:12 +03:00
parent 096ac9c251
commit c43d152d33
108 changed files with 92 additions and 243 deletions

View File

@ -0,0 +1,173 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
// ---------------------------------------------------------
// EXERCISE: Refactor
//
// Goal is refactoring the clock project by moving some of its parts to
// a new file in the same package.
//
// 1. Create a new file: placeholders.go
// 2. Move the placeholder type to placeholder.go
// 3. Move all the placeholders (zero to nine and the colon) to placeholder.go
// 4. Move the digits array to placeholders.go
//
// HINT
// + placeholders.go file should belong to main package as well
//
// To remember how to do so: Rewatch the "PART I — Packages, Scopes and Importing"
// section.
//
// + Short declaration won't work in the package scope.
// Remember why by watching: "Short Declaration: Package Scope" lecture
//
// + If you receive the following error and you don't know what to do watch:
// "Packages - Learn how to run multiple files" lecture.
//
// # command-line-arguments
// undefined: placeholder
// undefined: colon
//
// EXPECTED OUTPUT
// The same output before the refactoring.
// ---------------------------------------------------------
package main
import (
"fmt"
"time"
"github.com/inancgumus/screen"
)
func main() {
type placeholder [5]string
zero := placeholder{
"███",
"█ █",
"█ █",
"█ █",
"███",
}
one := placeholder{
"██ ",
" █ ",
" █ ",
" █ ",
"███",
}
two := placeholder{
"███",
" █",
"███",
"█ ",
"███",
}
three := placeholder{
"███",
" █",
"███",
" █",
"███",
}
four := placeholder{
"█ █",
"█ █",
"███",
" █",
" █",
}
five := placeholder{
"███",
"█ ",
"███",
" █",
"███",
}
six := placeholder{
"███",
"█ ",
"███",
"█ █",
"███",
}
seven := placeholder{
"███",
" █",
" █",
" █",
" █",
}
eight := placeholder{
"███",
"█ █",
"███",
"█ █",
"███",
}
nine := placeholder{
"███",
"█ █",
"███",
" █",
"███",
}
colon := placeholder{
" ",
" ░ ",
" ",
" ░ ",
" ",
}
digits := [...]placeholder{
zero, one, two, three, four, five, six, seven, eight, nine,
}
screen.Clear()
for {
screen.MoveTopLeft()
now := time.Now()
hour, min, sec := now.Hour(), now.Minute(), now.Second()
clock := [...]placeholder{
digits[hour/10], digits[hour%10],
colon,
digits[min/10], digits[min%10],
colon,
digits[sec/10], digits[sec%10],
}
for line := range clock[0] {
for index, digit := range clock {
// colon blink
next := clock[index][line]
if digit == colon && sec%2 == 0 {
next = " "
}
fmt.Print(next, " ")
}
fmt.Println()
}
time.Sleep(time.Second)
}
}

View File

@ -0,0 +1,48 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"time"
"github.com/inancgumus/screen"
)
func main() {
screen.Clear()
for {
screen.MoveTopLeft()
now := time.Now()
hour, min, sec := now.Hour(), now.Minute(), now.Second()
clock := [...]placeholder{
digits[hour/10], digits[hour%10],
colon,
digits[min/10], digits[min%10],
colon,
digits[sec/10], digits[sec%10],
}
for line := range clock[0] {
for index, digit := range clock {
// colon blink
next := clock[index][line]
if digit == colon && sec%2 == 0 {
next = " "
}
fmt.Print(next, " ")
}
fmt.Println()
}
time.Sleep(time.Second)
}
}

View File

@ -0,0 +1,95 @@
package main
type placeholder [5]string
var zero = placeholder{
"███",
"█ █",
"█ █",
"█ █",
"███",
}
var one = placeholder{
"██ ",
" █ ",
" █ ",
" █ ",
"███",
}
var two = placeholder{
"███",
" █",
"███",
"█ ",
"███",
}
var three = placeholder{
"███",
" █",
"███",
" █",
"███",
}
var four = placeholder{
"█ █",
"█ █",
"███",
" █",
" █",
}
var five = placeholder{
"███",
"█ ",
"███",
" █",
"███",
}
var six = placeholder{
"███",
"█ ",
"███",
"█ █",
"███",
}
var seven = placeholder{
"███",
" █",
" █",
" █",
" █",
}
var eight = placeholder{
"███",
"█ █",
"███",
"█ █",
"███",
}
var nine = placeholder{
"███",
"█ █",
"███",
" █",
"███",
}
var colon = placeholder{
" ",
" ░ ",
" ",
" ░ ",
" ",
}
var digits = [...]placeholder{
zero, one, two, three, four, five, six, seven, eight, nine,
}