package main
import (
"fmt"
"time"
)
func double(num int, ch chan int) {
time.Sleep(time.Second * 1)
ch <- num * 2
}
func main() {
// var ch chan int = make(chan int)
ch := make(chan int)
fmt.Println("Start!")
go double(10, ch)
time.Sleep(time.Second * 3)
fmt.Println("Finish!")
}
カテゴリー: Go言語
Go言語 インターフェース
package main
import "fmt"
type englishScoreType int
type mathScoreType int
func (e englishScoreType) isPassed() bool {
return e >= 80
}
func (m mathScoreType) isPassed() bool {
return m >= 70
}
type passChecker interface {
isPassed() bool
}
func showResult(score passChecker) {
if score.isPassed() {
fmt.Println("Pass!")
} else {
fmt.Println("Fail...")
}
}
func main() {
englishScore := englishScoreType(90)
mathScore := mathScoreType(50)
showResult(englishScore)
showResult(mathScore)
}
Go言語 構造体
package main
import "fmt"
type studentType struct {
name string
score scoreType
}
type scoreType struct {
subject string
points int
}
func (s scoreType) isPassed() bool {
if s.points >= 70 {
return true
} else {
return false
}
}
func main() {
taro := studentType{
name: "Taro",
score: scoreType{
subject: "Math",
points: 85,
},
}
fmt.Println(taro.name)
fmt.Println(taro.score.subject)
fmt.Println(taro.score.points)
fmt.Println(taro.score.isPassed())
}
Go言語 メソッド
package main
import "fmt"
type nameString string
func (n nameString)showInfo() {
fmt.Println(n)
}
func main() {
var productName nameString = "Banana"
productName.showInfo()
}
Go言語ポインタ
package main
import "fmt"
type student struct {
name string
subject string
score int
}
func addScore(pStudent *student) {
pStudent.score++
}
func main() {
taro := student{
name: "Taro",
subject: "Math",
score: 40,
}
addScore(&taro)
fmt.Println(taro.score)
}
Go言語 ジェネリクス
package main
import "fmt"
// func showIntThreeTimes(num int) {
// fmt.Println(num)
// fmt.Println(num)
// fmt.Println(num)
// }
// func showFloat64ThreeTimes(num float64) {
// fmt.Println(num)
// fmt.Println(num)
// fmt.Println(num)
// }
func showThreeTimes[T any](num T) {
fmt.Println(num)
fmt.Println(num)
fmt.Println(num)
}
func main() {
// showIntThreeTimes(3)
// showFloat64ThreeTimes(5.2)
showThreeTimes(3)
showThreeTimes(5.2)
}
Go言語 関数を変数に代入
package main
import "fmt"
// func triple(num int) int {
// return num * 3
// }
func main() {
// f := triple
f := func(num int) int {
return num * 3
}
fmt.Println(f(5))
}
Go言語 真偽値
package main
import "fmt"
func isPasswordValid(password string) bool {
if len(password) < 8 {
return false
} else {
return true
}
}
func main() {
fmt.Println(isPasswordValid("abc")) // false
fmt.Println(isPasswordValid("helloworld")) // true
}
Go言語 複数の値
package main
import "fmt"
func divide(a, b int) (int, int) {
return a / b, a % b
}
func main() {
quotient, remainder := divide(10, 3)
fmt.Println(quotient)
fmt.Println(remainder)
}
Go言語 switch
package main
import "fmt"
func main() {
var score int
fmt.Println("Score?")
fmt.Scanf("%d", &score)
if score >= 90 {
fmt.Println("A!")
} else if score >= 70 {
fmt.Println("B!")
} else {
fmt.Println("C!")
}
switch{
case score >= 90:
fmt.Println("A!")
case score >= 70:
fmt.Println("B!")
default:
fmt.Println("C!")
}
}