= 400 { fmt.Println(err) return errRequestFailed } return nil } func main11() { c := make(chan string) people := [3]string{"nico", "flynn", "a"} for _, person := range people { go isSexy(person"> = 400 { fmt.Println(err) return errRequestFailed } return nil } func main11() { c := make(chan string) people := [3]string{"nico", "flynn", "a"} for _, person := range people { go isSexy(person"> = 400 { fmt.Println(err) return errRequestFailed } return nil } func main11() { c := make(chan string) people := [3]string{"nico", "flynn", "a"} for _, person := range people { go isSexy(person">
package main
import (
"errors"
"fmt"
"net/http"
"time"
)
var (
errRequestFailed = errors.New("Request Failed")
)
var results = map[string]string{}
func main() {
urls := []string{
"<https://www.airbnb.com/>",
"<https://www.google.com/>",
"<https://www.amazon.com/>",
"<https://www.reddit.com/>",
"<https://www.google.com/>",
"<https://soundcloud.com/>",
"<https://www.facebook.com/>",
"<https://www.instagram.com/>",
"<https://academy.nomadcoders.co/>",
}
for _, url := range urls {
result := "OK"
err := hitURL(url)
if err != nil {
result = "FAILED"
}
results[url] = result
}
for url, result := range results {
fmt.Println(url, result)
}
}
func hitURL(url string) error {
fmt.Println("Checking:", url)
res, err := http.Get(url)
if err != nil || res.StatusCode >= 400 {
fmt.Println(err)
return errRequestFailed
}
return nil
}
func main11() {
c := make(chan string)
people := [3]string{"nico", "flynn", "a"}
for _, person := range people {
go isSexy(person, c)
}
for i := 0; i < len(people); i++ {
fmt.Println(<-c)
}
//go sexyCount("Lee")
//sexyCount("Kim")
// result := <-c 이런식으로 channel을 받아야 함
//fmt.Println(<-c)
//fmt.Println(<-c)
}
func isSexy(person string, c chan string) {
time.Sleep(time.Second * 5)
c <- person + " is sexy"
}
func sexyCount(person string) {
for i := 0; i < 5; i++ {
fmt.Println(person, "is sexy", i)
time.Sleep(time.Second)
}
}
package main
import (
"fmt"
"gin-practice/banking"
mydict "gin-practice/dict"
"github.com/gin-gonic/gin"
"log"
"net/http"
)
func main1() {
r := gin.Default() // default settings
r.GET("", func(c *gin.Context) {
c.String(http.StatusOK, "hello world!") // 1: http status,
// 2: 응답으로 보내고 싶은 데이터
}) // 1: url, 2: handler function
r.Run("localhost:8080") // api를 호스트할 url과 포트번호
}
func Test() (a int, b string, c string) {
a = 1
b = "a"
c = b
return
}
func main2() {
fmt.Println("Hello World!")
one, _, b := Test()
fmt.Println(one, b)
}
func main3() {
account := banking.NewAccount("jeongho")
account.RealDeposit(10)
fmt.Println(account)
fmt.Println(account.Balance())
err := account.Withdraw(20)
if err != nil {
fmt.Println(err)
log.Fatalln(err) // 이런식으로 에러 핸들링 필요
}
fmt.Println(account.Balance())
}
func main4() {
dictionary := mydict.Dictionary{"first": "First word"}
dictionary["hello"] = "hello"
fmt.Println(dictionary)
definition, err := dictionary.Search("first")
if err != nil {
fmt.Println(err)
}
fmt.Println(definition)
}
func main5() {
dictionary := mydict.Dictionary{"first": "First word"}
err := dictionary.Addword("hello", "greeting")
if err != nil {
fmt.Println(err)
}
definition, err1 := dictionary.Search("hello")
if err1 != nil {
fmt.Println(err1)
}
fmt.Println(definition)
}
func main6() {
dictionary := mydict.Dictionary{"first": "First word"}
baseWord := "hello"
dictionary.Addword(baseWord, "First")
err := dictionary.UpdateWord(baseWord, "Second")
if err != nil {
fmt.Println(err)
}
word, _ := dictionary.Search(baseWord)
fmt.Println(word)
}
func main7() {
dictionary := mydict.Dictionary{"first": "First word"}
baseWord := "hello"
dictionary.Addword(baseWord, "First")
dictionary.Search(baseWord)
dictionary.DeleteWord(baseWord)
word, err := dictionary.Search(baseWord)
if err != nil {
fmt.Println(err)
}
fmt.Println(word)
}
package mydict
import (
"errors"
)
var (
errNotFound = errors.New("Word Not Found")
errCannotUpdate = errors.New("Cannot Update")
errWordExists = errors.New("Word Exists")
)
// Dictionary type
type Dictionary map[string]string
func (d Dictionary) Search(word string) (string, error) {
value, exists := d[word] // d[word] -> 값, 존재 여부 리턴
if exists {
return value, nil
}
return "", errNotFound
}
func (d Dictionary) Addword(word, def string) error {
_, err := d.Search(word)
if err == nil {
return errWordExists
}
d[word] = def
return nil
}
func (d Dictionary) UpdateWord(word, def string) error {
_, err := d.Search(word)
if err != nil {
return errCannotUpdate
}
d[word] = def
return nil
}
func (d Dictionary) DeleteWord(word string) error {
_, err := d.Search(word)
if err != nil {
return errCannotUpdate
}
delete(d, word)
return nil
}
package banking
import (
"errors"
"fmt"
)
type Account struct {
owner string // owner -> private, Owner -> public 대소문자로 접근제한자 구분 변수, 메소드 모두 동일
balance int
}
var errNoMoney = errors.New("Can't withdraw you are poor")
func NewAccount(owner string) *Account { // Object를 리턴하는것 (복사본 리턴 X)
account := Account{owner: "jeongho", balance: 0}
return &account
}
func (a Account) Deposit(amount int) { // function이 아닌 method이다 (a Account) -> '리시버'라고 부름
a.balance += amount // 이렇게 되면 복사된 어느 Account의 Balance만 증가됨 (깊은 복사 느낌)
}
func (a *Account) RealDeposit(amount int) { // 포인터 리시버
a.balance += amount // 얕은 복사 느낌으로 Deposit method를 호출한 Account에 Balance를 더해줌
}
func (a Account) Balance() int {
return a.balance
}
func (a Account) Owner() string {
return a.owner
}
func (a *Account) Withdraw(amount int) error { // Go는 Exception 존재 X -> Error Handling 코드 작성 필요
if a.balance < amount {
return errNoMoney
}
a.balance -= amount
return nil
}
func (a Account) String() string { // Go가 내부적으로 호출하는 메소드 중 하나 String
return fmt.Sprint(a.Owner(), "'s account.\\nHas:", a.Balance()) // 이런식으로 정의 가능
}