Files

80 lines
2.8 KiB
Go
Raw Permalink Normal View History

2019-03-04 20:30:18 +03:00
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
2019-10-30 19:34:44 +03:00
// For more tutorials : https://learngoprogramming.com
// In-person training : https://www.linkedin.com/in/inancgumus/
// Follow me on twitter: https://twitter.com/inancgumus
2019-03-04 20:30:18 +03:00
package main
import (
"fmt"
"strings"
)
func main() {
// --- Correct Lyric ---
// yesterday all my troubles seemed so far away
// now it looks as though they are here to stay
2021-05-01 13:21:56 +03:00
// oh I believe in yesterday
lyric := strings.Fields(`all my troubles seemed so far away oh I believe in yesterday now it looks as though they are here to stay`)
2019-03-04 20:30:18 +03:00
// ------------------------------------------------------------------------
// #1: Prepend "yesterday" to `lyric`
// ------------------------------------------------------------------------
//
// --- BEFORE ---
2021-05-01 13:21:56 +03:00
// all my troubles seemed so far away oh I believe in yesterday
2019-03-04 20:30:18 +03:00
//
// --- AFTER ---
2021-05-01 13:21:56 +03:00
// yesterday all my troubles seemed so far away oh I believe in yesterday
2019-03-04 20:30:18 +03:00
//
// (warning: allocates a new backing array)
//
lyric = append([]string{"yesterday"}, lyric...)
// ------------------------------------------------------------------------
2019-08-18 15:08:32 +03:00
// #2: Put the words to the correct positions in the `lyric` slice.
2019-03-04 20:30:18 +03:00
// ------------------------------------------------------------------------
//
2021-05-01 13:21:56 +03:00
// yesterday all my troubles seemed so far away oh I believe in yesterday
2019-03-04 20:30:18 +03:00
// | |
// v v
// index: 8 pos: 13
//
const N, M = 8, 13
// --- BEFORE ---
//
2021-05-01 13:21:56 +03:00
// yesterday all my troubles seemed so far away oh I believe in yesterday now it looks as though they are here to stay
2019-03-04 20:30:18 +03:00
//
// --- AFTER ---
2021-05-01 13:21:56 +03:00
// yesterday all my troubles seemed so far away oh I believe in yesterday now it looks as though they are here to stay oh I believe in yesterday
2019-03-04 20:30:18 +03:00
// ^
//
// |
// +--- duplicate
//
// (warning: allocates a new backing array)
lyric = append(lyric, lyric[N:M]...)
//
// --- BEFORE ---
2021-05-01 13:21:56 +03:00
// yesterday all my troubles seemed so far away oh I believe in yesterday now it looks as though they are here to stay oh I believe in yesterday
2019-03-04 20:30:18 +03:00
//
// --- AFTER ---
2021-05-01 13:21:56 +03:00
// yesterday all my troubles seemed so far away now it looks as though they are here to stay oh I believe in yesterday
2019-03-04 20:30:18 +03:00
//
// (does not allocate a new backing array because cap(lyric[:N]) > M)
//
lyric = append(lyric[:N], lyric[M:]...)
// ------------------------------------------------------------------------
// #3: Print
// ------------------------------------------------------------------------
fmt.Printf("%s\n", lyric)
}