반응형
<for>
func superAdd(numbers ...int) int {
total := 0
for _, number := range numbers {
total += number
}
return total
}
func main(){
superAdd(1,2,3,4,5)
}
<if else>
func canIDrink(age int) bool {
koreanAge := age + 2
if koreanAge <18 {
return false
}
return ture
}
<array>
func main() {
names := []string{"nico", "lynn", "dal"}
names = append(names, "flynn")
fmt.Println(names)
}
<Maps>
func main() {
nico := map[string]string{"name":"nico", "age":"12"}
for key, value := range nico {
fmt.Println(key, value)
}
}
<Struct>
type person struct {
name string
age int
favFood []string
}
func main(){
favFood := []string{"kimchi", "ramen"}
nico := person{name: "nico", age:18, favFood: favFood}
fmt.Println(nico.name)
}
<Url Checker>
package main
import (
"errors"
"fmt"
"net/http"
)
var errRequestFailed = errors.New("Request failed")
func main() {
var results = make(map[string]string)
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)
resp, err := http.Get(url)
if err != nil || resp.StatusCode >= 400 {
fmt.Println(err, resp.StatusCode)
return errRequestFailed
}
return nil
}
<Go routines>
import (
"fmt"
"time"
)
func main() {
go sexyCount("nico")
sexyCount("flynn")
}
func sexyCount(person string){
for i :=0; i < 10; i++{
fmt.Println(person, "is sexy", i)
time.Sleep(time.Second)
}
}
--> go routine은 main function이 돌아가는 동안에만 유효하다.
--> main function 은 go routine이 완료되길 기다려주지 않는다.
<Channel>
func main(){
c := make(chan bool)
people := [2]string{"nico", "flynn"}
for _, person := range people {
go isSexy(person, c)
}
for i:=0;i<len(people);i++{
fmt.Println(<-c)
}
}
func isSexy(person string, c chan bool){
time.Sleep(time.Seconds * 5)
c <- true
}
<Url Check with go routine>
package main
import (
"errors"
"fmt"
"net/http"
)
type requestResult struct {
url string
status string
}
var errRequestFailed = errors.New("Request failed")
func main() {
results := make(map[string]string)
c := make(chan requestResult)
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 {
go hitURL(url, c)
}
for i := 0; i < len(urls); i++ {
result := <-c
results[result.url] = result.status
}
for url, status := range results {
fmt.Println(url, status)
}
}
func hitURL(url string, c chan<- requestResult) {
resp, err := http.Get(url)
status := "OK"
if err != nil || resp.StatusCode >= 400 {
status = "FAILED"
}
c <- requestResult{url: url, status: status}
}
반응형
'Tech > Language' 카테고리의 다른 글
[python] wget download (0) | 2021.03.11 |
---|---|
golang 환경설정 (0) | 2020.10.12 |