forked from zdiyax/onetech-golang-syllabus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zhannur Diyas
committed
Jul 7, 2019
1 parent
3825044
commit 25868ac
Showing
7 changed files
with
255 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
const ( | ||
zero = 1 << (8 + iota) | ||
one | ||
two | ||
three | ||
nine | ||
) | ||
|
||
func main() { | ||
d := Dog{} | ||
|
||
d.name = "Mukhtar" | ||
d.isReal = true | ||
d.sound = "woof woof" | ||
|
||
fmt.Println(d) | ||
|
||
d.makeSound() | ||
|
||
fmt.Println(zero) | ||
fmt.Println(one) | ||
fmt.Println(two) | ||
fmt.Println(three) | ||
fmt.Println(nine) | ||
} | ||
|
||
type Animal struct { | ||
name string | ||
sound string | ||
isReal bool | ||
} | ||
|
||
type Dog struct { | ||
Animal | ||
} | ||
|
||
func (a *Animal) makeSound() { | ||
fmt.Println(a.sound) | ||
fmt.Println(a.sound) | ||
fmt.Println(a.sound) | ||
} | ||
|
||
func (d *Dog) makeSound() { | ||
fmt.Println(d.sound + " POLYMORPHISM " + d.sound) | ||
fmt.Println(d.sound + " POLYMORPHISM " + d.sound) | ||
fmt.Println(d.sound + " POLYMORPHISM " + d.sound) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
//maps in go are hashmaps | ||
|
||
func main() { | ||
m := make(map[string]string) | ||
//декларация #2 | ||
//var m2 = map[string]string{} | ||
|
||
m["vasya"] = "123213" | ||
m["kolya"] = "321321" | ||
m["petya"] = "424242" | ||
m["egor"] = "000000" | ||
|
||
//foreach | ||
for key, value := range m { | ||
fmt.Println(key + " " + value) | ||
} | ||
|
||
//проверка есть ли значение в map | ||
if val, ok := m["kolya"]; ok { | ||
fmt.Println("val is : ", val) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
//аллоцировали память для структуры ConnectionPool | ||
connectionPool := ConnectionPool{} | ||
|
||
//проинициализировали его пятью соединениями | ||
connectionPool.init(5) | ||
|
||
userId1 := "qwe123" | ||
userId2 := "qwe124" | ||
userId3 := "qwe125" | ||
|
||
//3 наших юзера с разными userId забрали по соединению | ||
connectionPool.getConnection(userId1) | ||
connectionPool.getConnection(userId2) | ||
connectionPool.getConnection(userId3) | ||
|
||
//print должен показывать 2 свободных и 3 занятых соединения | ||
fmt.Println(connectionPool) | ||
|
||
connectionPool.returnConnection(userId1) | ||
|
||
//print должен показывать 3 свободных и 2 занятых соединения | ||
fmt.Println(connectionPool) | ||
} | ||
|
||
type ConnectionPool struct { | ||
//connection -> user_id | ||
connections map[Connection]string | ||
// Connectionx1 -> "qwe123" | ||
// Connectionx2 -> "qwe124" | ||
// Connectionx3 -> "qwe125" | ||
} | ||
|
||
type Connection struct { | ||
Id string | ||
|
||
//"50" - 50 секунд | ||
Timeout string | ||
} | ||
|
||
func (c *ConnectionPool) getConnection(userId string) Connection { | ||
panic("implement me!") | ||
} | ||
|
||
func (c *ConnectionPool) returnConnection(userId string) { | ||
panic("implement me!") | ||
} | ||
|
||
func (c *ConnectionPool) init(size int) { | ||
panic("implement me!") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
func main() { | ||
var sortThis []int | ||
|
||
n := 400 | ||
|
||
for i := 0; i < n; i++ { | ||
sortThis = append(sortThis, rand.Intn(100)) | ||
} | ||
|
||
fmt.Println(sortThis) | ||
|
||
bubbleSort(sortThis) | ||
|
||
fmt.Println(sortThis) | ||
} | ||
|
||
func bubbleSort(values []int) { | ||
startTime := time.Now() | ||
|
||
for i := 0; i < len(values)-1; i++ { | ||
for j := i + 1; j < len(values); j++ { | ||
if values[j] < values[i] { | ||
values[j], values[i] = values[i], values[j] | ||
} | ||
} | ||
} | ||
|
||
dif := time.Since(startTime) | ||
fmt.Println(dif) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
) | ||
|
||
func main() { | ||
deck := Deck{ | ||
cards: []Card{ | ||
{"Туз", "Пики"}, | ||
{"Дама", "Треф"}, | ||
{"Король", "Буби"}, | ||
{"Шестерка", "Черви"}, | ||
}, | ||
} | ||
|
||
fmt.Println(deck) | ||
|
||
deck.shuffle() | ||
|
||
fmt.Println(deck) | ||
} | ||
|
||
type Card struct { | ||
value string | ||
colour string | ||
} | ||
|
||
type Deck struct { | ||
cards []Card | ||
} | ||
|
||
func (d *Deck) shuffle() { | ||
for i := len(d.cards) - 1; i > 0; i-- { | ||
j := rand.Intn(len(d.cards) - 1) | ||
d.cards[i], d.cards[j] = d.cards[j], d.cards[i] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
### Домашка #3 | ||
|
||
------------------------ | ||
|
||
* Закончить [connection pool](03_pool.go) | ||
|
||
Что произойдет если пользователь запрашивает соединение, а пул соединений пуст? | ||
|
||
Что произойдет если для пользователя в данный момент нет свободного соединения? | ||
|
||
Добавить возможность расширять пул __динамически__. К примеру, если все 5 соединений заняты в данный момент, увеличить пул соединений в 2 раза. | ||
|
||
Что произойдет если 2 пользователя одновременно запросят соединение? | ||
|
||
---------------------------- | ||
* Задача на композицию. | ||
|
||
Создать структуру птицы. Для птицы структуру крыла и хвоста. Крыло должно иметь поле длина и площадь. Хвост должен иметь длину и ширину. Помимо этого, у птицы должны быть поля окрас и вес. | ||
|
||
Создать несколько птиц с разными характеристиками используя разные инициализации. | ||
______ |