Files
01-get-started
02-write-your-first-program
03-packages-and-scopes
04-statements-expressions-comments
05-write-your-first-library-package
06-variables
07-printf
08-numbers-and-strings
09-go-type-system
10-constants
11-if
12-switch
13-loops
14-arrays
15-project-retro-led-clock
16-slices
17-project-empty-file-finder
18-project-bouncing-ball
19-strings-runes-bytes
20-project-spam-masker
21-project-text-wrapper
22-maps
23-input-scanning
24-structs
01-intro
02-basics
03-compare-assign
main.go
04-embedding
05-project-log-parser-structs
06-encoding
07-decoding
08-decoding-2
exercises
questions
25-functions
26-pointers
advfuncs
assets
concurrency
etc
first
interfaces
logparser
magic
magicpanic
translation
x-tba
.gitignore
LICENSE
README.md
go.mod
go.sum
main.go
2019-10-30 19:41:13 +03:00

96 lines
2.2 KiB
Go

// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
// For more tutorials : https://learngoprogramming.com
// In-person training : https://www.linkedin.com/in/inancgumus/
// Follow me on twitter: https://twitter.com/inancgumus
package main
import "fmt"
// #1b: create the song struct type
type song struct {
title, artist string
}
// #5: structs can contain other structs
type playlist struct {
genre string
// songTitles []string
// songArtist []string
// #6: include a slice of song structs
songs []song
}
func main() {
// #1: create two struct values with the same type
song1 := song{title: "wonderwall", artist: "oasis"}
song2 := song{title: "super sonic", artist: "oasis"}
fmt.Printf("song1: %+v\nsong2: %+v\n", song1, song2)
// #4: structs are copied
// song1 = song2
// #3: structs can be compared
if song1 == song2 {
// #2: struct comparison works like this
// if song1.title == song2.title &&
// song1.artist == song2.artist {
fmt.Println("songs are equal.")
} else {
fmt.Println("songs are not equal.")
}
// #8
songs := []song{
// #7b: you don't have to type the element types
{title: "wonderwall", artist: "oasis"},
{title: "supersonic", artist: "oasis"},
}
// #7: a struct can include another struct
rock := playlist{
genre: "indie rock",
songs: songs,
}
// #9: you can't compare struct values that contains incomparable fields
// you need to compare them manually
// clone := rock
// if rock.songs == clone {
// }
// if songs == songs {
// #11: song is a clone, it cannot change the original struct value
song := rock.songs[0]
song.title = "live forever"
// #11c: directly set the original one
rock.songs[0].title = "live forever"
// #11b
fmt.Printf("\n%+v\n%+v\n", song, rock.songs[0])
// #10: printing
fmt.Printf("\n%-20s %20s\n", "TITLE", "ARTIST")
for _, s := range rock.songs {
// s := rock.songs[i]
// #12b: s is a copy inside because struct values are copied
s.title = "destroy"
fmt.Printf("%-20s %20s\n", s.title, s.artist)
}
// #12
fmt.Printf("\n%-20s %20s\n", "TITLE", "ARTIST")
for _, s := range rock.songs {
fmt.Printf("%-20s %20s\n", s.title, s.artist)
}
}