Slide 1

Slide 1 text

(Pݚमୈظ %": :VUBLB,BUP 
 3% %&4".*4 %&4".*4ࣾ಺ݚम

Slide 2

Slide 2 text

ݚमܭը ճ ೔࣍ ༧ఆ λΠτϧ ୈճ Ր (Pͷجຊ ୈճ Ր ؀ڥߏஙͱ(JU (JU)VCͷجຊ ୈճ Ր جຊߏจ ୈճ Ր ߏ଄ମɺεϥΠεɺϚοϓ ୈճ Ր ඪ४ϥΠϒϥϦ͸΍Ί͙Γ ୈճ Ր ϞδϡʔϧγεςϜͱ֎෦ϥΠϒϥϦ ୈճ Ր )FSPLV(Pೖ໳ ୈճ Ր "84-BNCEB(Pೖ໳ ୈճ Ր ฒߦॲཧϓϩάϥϛϯά ୈճ Ր ୯ମςετٕ๏ ୈճ Ր σʔλϕʔεΞΫηε ୈճ Ր 'ZOFʹΑΔ(6*ΞϓϦέʔγϣϯ։ൃ ୈճ Ր ࣾ಺(PϓϩδΣΫτͷίʔυղઆ

Slide 3

Slide 3 text

˞ લճͷ13ͷ 
 ղઆλΠϜ

Slide 4

Slide 4 text

ຊ೔ͷΰʔϧ w 🚩ΰʔϧ w UFTUJOHඪ४ϥΠϒϥϦͷ༷ʑͳػೳΛֶͿ w ςʔϒϧۦಈςετ౳ͷύλʔϯΛशಘ͢Δ w ❌ΰʔϧͰ͸ͳ͍ w ୯ମςετͷྖҬΛ௒͑Δ༷ʑͳςετͷ࣮ݱ

Slide 5

Slide 5 text

ςετͷجຊ

Slide 6

Slide 6 text

Ͱ͖Δ͜ͱ w ςετؔ਺ͷ࣮૷ͱ࣮ߦ w ϕϯνϚʔΫؔ਺ͷ࣮૷ͱ࣮ߦ w ίʔυྫؔ਺ͷ࣮૷ͱ࣮ߦɺHPEPD΁ͷܝࡌ w ΧόϨοδͷܭଌ HPUFTUDPWFSΦϓγϣϯ w ϓϩϑΝΠϦϯά HPUFTUNFNQSP fi MF౳ͷΦϓγϣϯ w σʔλڝ߹ͷݕग़ HPUFTUSBDFΦϓγϣϯ w ͳͲͳͲʜ

Slide 7

Slide 7 text

໨ࢦ͍ͯ͠Δઃܭ w ςετ͸શͯ(Pͷجຊߏจ if, for, struct౳ Ͱॻ͘ w ςετͷͨΊͷ৽ͨͳπʔϧ΍ه๏Λඞཁͱ͠ͳ͍ w Ξαʔγϣϯ΍ϔϧύʔؔ਺͸ͳ͍ w جຊʹཱͪฦΓɺྑ͍ςετɺྑ͍ࣦഊϝοηʔδΛجຊ ߏจͰ͔ͬ͠Γॻ͘ͷ͕େࣄ (P'"2 
 l8IZEPFT(POPUIBWFBTTFSUJPOT lٴͼ 
 l8IFSFJTNZGBWPSJUFIFMQFSGVODUJPOGPSUFTUJOH zΑΓ IUUQTHPMBOHPSHEPDGBR

Slide 8

Slide 8 text

ϧʔϧ ςετίʔυ͸ʮ@UFTUHPʯͰऴΘΔϑΝΠϧʹ࣮ ૷͢Δ ςετͷ࣮ߦ͸ʮHPUFTUʯαϒίϚϯυΛ༻͍Δ ςετίʔυ͸ී௨ʹϏϧυ HPCVJME ͢Δࡍʹ͸ί ϯύΠϧ͞Εͳ͍ HPUFTUͰ࣮ߦͯ͠΋Β͍͍ͨؔ਺͸໋໊نଇͱγ άχνϟͷنఆʹै͏ඞཁ͕͋Δ

Slide 9

Slide 9 text

ςετؔ਺

Slide 10

Slide 10 text

࡞Γํ w ҎԼͷϧʔϧʹैͬͨؔ਺Λ࣮૷͢ΔͱɺHP UFTUͰؔ਺Λ࣮ߦͯ݁͠ՌΛϨϙʔτͯ͘͠ΕΔ w UFTUJOHύοέʔδΛΠϯϙʔτ w ࣍ͷؔ਺γάχνϟΛ࣋ͭ 
 func TestName(t *testing.T) { … } w Name͸େจࣈͰ࢝·Δςετͷ໊લ

Slide 11

Slide 11 text

΍ͬͯΈΑ͏ 74$PEFΛىಈ͠ݚमϦϙδτϦΛ։͖·͢ Ϣʔβʔ໊ϑΥϧμҎԼʹʮEBZUFTUʯϑΥϧμΛ࡞੒͠ɺͦͷதʹ ҎԼͷ಺༰ͰʮUIJHPʯͱʮUIJ@UFTUHPʯΛ࡞੒͠·͢ package th i // THI ͸Թ౓ (℃) ͱ࣪౓ (%) ͔Β THI (ෆշࢦ਺) ܭࢉ͠·͢ɻ func THI(temperature, humidity float64) float64 { return (0.8 * temperature) + humidity*(temperature-14.4)/100 + 46. 4 } package th i import "testing " func TestTHI(t *testing.T) { temperature, humidity, expected := 25.2, 65.0, 73. 5 actual := THI(temperature, humidity ) if expected != actual { t.Errorf("THI(%v, %v) = %v (ظ଴஋: %v)", temperature, humidity, actual, expected ) } } t h i . g o t h i _ t e s t . g o

Slide 12

Slide 12 text

΍ͬͯΈΑ͏ --- FAIL: TestTHI (0.00s) thi_test.go:9: THI(25.2, 65) = 73.58 (ظ଴஋: 73.5) FAIL exit status 1 FAIL github.com/u-motion/go-training-1/mikan/day10-test1 0.137s ʮUIJHPʯΛӈΫϦοΫͯ͠ϝχϡʔ͔Β౷߹λʔϛφϧΛ։͖ɺ 
 ʮHPUFTUʯʻ&OUFSʼͱೖྗ͍ͯͩ͘͠͞ ʮUIJ@UFTUHPʯͷexpectedͷ஋Λʹमਖ਼͠ɺ࠶࣮ߦ͍ͯͩ͘͠͞ PASS ok github.com/u-motion/go-training-1/mikan/day10-test1 0.284s ੒ޭྫ

Slide 13

Slide 13 text

ςʔϒϧۦಈςετ w Α͘࢖ΘΕΔύλʔϯςʔϒϧۦಈ UBCMFESJWFO ςετ˞ w ೖྗɾظ଴஋ͷ૊Έ߹ΘͤΛෳ਺༻ҙͯ͠ϧʔϓͰճ͢ελΠϧ ˞ڭՊॻͰ͸ 
 ʮදۦಈʯͱ 
 ༁͞Ε͍ͯΔ func TestTHI(t *testing.T) { var tests = []struct { temperature float6 4 humidity float6 4 expected float6 4 } { {25.2, 65.0, 73.58} , {-33.0, 100.0, -27.4} , {40, 0.0, 78.4} , } for i, test := range tests { actual := THI(test.temperature, test.humidity ) if test.expected != actual { t.Errorf("#%d: THI(%v, %v) = %v (ظ଴஋: %v)", i+1 , test.temperature, test.humidity, actual, test.expected ) } } } ςʔϒϧͷ֤ߦͷఆٛ lςʔϒϧz ϧʔϓͰ֤ߦΛճ͢

Slide 14

Slide 14 text

ΧόϨοδ

Slide 15

Slide 15 text

ΧόϨοδܭଌ w ςετίʔυ͕ຊମίʔυΛͲͷఔ౓໢ཏ͍ͯ͠Δ͔Λܭଌ͢ Δ͜ͱ͕Ͱ͖Δ w ߦΧόϨοδɺ໋ྩΧόϨοδ $ ɺ෼ذΧόϨοδ $ ɺ ৚݅ΧόϨοδ $ ͳͲͷࢦඪ͕͋Δ w HPUFTUαϒίϚϯυʹDPWFSΦϓγϣϯΛ෇͚ΔͱΧόϨο δ $ Λܭଌ͢Δ w 74$PEFͰ໢ཏ͍ͯ͠ΔՕॴɾ͍ͯ͠ͳ͍ՕॴΛදࣔͨ͠Γɺ ΧόϨοδ݁ՌΛϑΝΠϧʹग़ྗͯ͠πʔϧͰӾཡ͢Δ͜ͱ΋ Ͱ͖Δ

Slide 16

Slide 16 text

΍ͬͯΈΑ͏ ͖͞΄ͲͷUIJHPʹҎԼͷ෼ذΛ௥Ճ͠·͢ 
 
 ౷߹λʔϛφϧͰʮHPUFTUDPWFSʯʻ&OUFSʼΛೖྗ͠ɺ 
 ΧόϨοδΛ֬ೝ͠·͢ 74$PEFͷϝχϡʔͰ<දࣔ><ίϚϯυύϨοτʜ>Λબ୒ɺೖྗ ཝʹʮHPUFTUDPWFSʯ (P5PHHMF5FTU$PWFSBHFJO$VSSFOU 1BDLBHF ʻ&OUFSʼΛೖྗ͠·͢ ࠶࣮ߦͰݩʹ໭Γ·͢ if humidity < 0 || humidity > 100 { return math.NaN() } ˞ศٓతʹ/B/Λ࢖༻ɺ 
 FSSPSΛฦ͢΄͏͕ϕλʔ ྘͕໢ཏͨ͠Օॴɺ੺͕໢ཏ͍ͯ͠ͳ͍Օॴ

Slide 17

Slide 17 text

HPUPPMDPWFS ౷߹λʔϛφϧͰʮHPUFTUDPWFSQSP fi MFDPVUʯʻ&OUFSʼ 
 ʮHPUPPMDPWFSIUNMDPVUʯʻ&OUFSʼΛೖྗ͠·͢ ϒϥ΢β্ཱ͕͕ͪΓ)5.-ϑΝΠϧ͕։͔ΕΔͷͰɺ݁Ռ Λ֬ೝ͠·͢

Slide 18

Slide 18 text

ϕϯνϚʔΫؔ਺

Slide 19

Slide 19 text

ϕϯνϚʔΫؔ਺ w ҎԼͷϧʔϧʹैͬͨؔ਺Λ࣮૷͢ΔͱɺʮHPUFTU CFODIʯͰؔ਺Λ࣮ߦͯ݁͠ՌΛϨϙʔτͯ͘͠Ε Δ w UFTUJOHύοέʔδΛΠϯϙʔτ w ࣍ͷؔ਺γάχνϟΛ࣋ͭ 
 func BenchmarkName(b *testing.B) { … } w Name͸େจࣈͰ࢝·ΔϕϯνϚʔΫͷ໊લ

Slide 20

Slide 20 text

࣮ߦྫ func BenchmarkTHI(b *testing.B) { for i := 0; i < b.N; i++ { THI(10.0, 50.0 ) } } func BenchmarkTxH(b *testing.B) { for i := 0; i < b.N; i++ { TxH(10.0, 50.0 ) } } Թ౓º࣪౓ ۭؾ೤ྔࢦ਺ Λฦ͚ͩ͢ͷ؆୯ͳؔ਺ ઌ΄Ͳ࡞ͬͨ5)* ؔ਺ % go test -bench . goos: darwin goarch: amd64 pkg: github.com/u-motion/go-training-1/mikan/day10-thi cpu: Intel(R) Core(TM) m5-6Y54 CPU @ 1.10GHz BenchmarkTHI-4 1000000000 0.3862 ns/op BenchmarkTxH-4 1000000000 0.3776 ns/op PASS ok github.com/u-motion/go-training-1/mikan/day10-thi 0.999s

Slide 21

Slide 21 text

࣮ߦྫ func BenchmarkTHI(b *testing.B) { for i := 0; i < b.N; i++ { THI(10.0, 50.0 ) } } func BenchmarkTxH(b *testing.B) { for i := 0; i < b.N; i++ { TxH(10.0, 50.0 ) } } Թ౓º࣪౓ ۭؾ೤ྔࢦ਺ Λฦ͚ͩ͢ͷ؆୯ͳؔ਺ ઌ΄Ͳ࡞ͬͨ5)* ؔ਺ % go test -bench . goos: darwin goarch: amd64 pkg: github.com/u-motion/go-training-1/mikan/day10-thi cpu: Intel(R) Core(TM) m5-6Y54 CPU @ 1.10GHz BenchmarkTHI-4 1000000000 0.3862 ns/op BenchmarkTxH-4 1000000000 0.3776 ns/op PASS ok github.com/u-motion/go-training-1/mikan/day10-thi 0.999s ͸(0."9130$4ͷ஋ ԯճ࣮ߦͨ͠ ҆ఆͨ͠ܭଌ ʹඞཁͳ਺ΛࣗಈͰਪఆ͍ͯ͠Δ ଌఆ݁Ռ ฏۉ஋ ܭࢉ͕୯७ͳ5Y) ͷ΄͏͕ͪΐͬͱ͚ͩ଎͍ʂ

Slide 22

Slide 22 text

ίʔυྫؔ਺

Slide 23

Slide 23 text

ίʔυྫؔ਺ w ҎԼͷϧʔϧʹैͬͨؔ਺Λ࣮૷͢ΔͱɺʮHPUFTUʯͰؔ਺Λ࣮ߦ͠ ͯ݁ՌΛϨϙʔτͯ͘͠ΕΔ w ࣍ͷؔ਺γάχνϟͱίϝϯτΛ࣋ͭ 
 func ExampleName() {
 (ඪ४ग़ྗ͕͋Δίʔυ)
 // Output:
 // (ඪ४ग़ྗͷظ଴͢Δ݁Ռ)
 } w Name͸େจࣈͰ࢝·Δίʔυྫͷ໊લ w هड़ͨ͠ίʔυ͸HPEPDʹ΋දࣔ͞ΕΔ 
 ˞HPEPDʹࡌͤΔ͜ͱ͚͕ͩओͳ໨తͳΒඪ४ग़ྗ͸ͳͯ͘΋ྑ͍

Slide 24

Slide 24 text

΍ͬͯΈΑ͏ ͖͞΄ͲͷUIJ@UFTUHPʹҎԼͷίʔυΛ௥Ճ͠·͢ 
 
 
 ౷߹λʔϛφϧͰʮHPUFTUʯʻ&OUFSʼΛೖྗ͠ɺࣦഊ͢Δͷ Λ֬ೝ͠·͢ Λʹमਖ਼͠ɺ΋͏Ұ౓ςετΛ࣮ߦͯ͠੒ޭʹͳ Δ͜ͱΛ֬ೝ͠·͢ func ExampleTHI() { fmt.Println(THI(20.5, 50.0) ) // Output : // 65. 8 } 見 た 目 は 文字通 り コ ー ド 例 だ け ど 、 こ れ も 立派 な テ ス ト コ ー ド !
 コ ー ド 例 で 触 った 部分 は カ バ レ ッ ジ 計測 に も 反映 さ れ ま す

Slide 25

Slide 25 text

HPEPDͷ֬ೝ ҎԼͷίϚϯυͰHPEPDͱ͍͏ίϚϯυϥΠϯπʔϧΛಋೖ͠·͢ 
 go install golang.org/x/tools/cmd/godoc@latest
 
 (࣮ߦόΠφϦ͕ <ϗʔϜσΟϨΫτϦ>/go/bin/ ҎԼʹ֨ೲ͞Ε·͢) ౷߹λʔϛφϧͰʮ~\go\bin\godocʯʻ5BCʼʻ&OUFSʼΛ࣮ߦ͠·͢˞ ϒϥ΢βͰIUUQMPDBMIPTUʹΞΫηε͠·͢ ࣗ෼͕࡞ͬͨίʔυͷυΩϡϝϯςʔγϣϯΛ୳͠·͢ 5IJSE1BSUZηΫγϣϯ 
 㸉HJUIVCDPN 
 ɹ㸉VNPUJPO ɹɹ㸉HPUSBJOJOH ɹɹɹ㸉Ϣʔβʔ໊ ɹɹɹɹ㸉EBZUFTU ˞$PNNBOE1SPNQUͷ৔߹ 
 ɹ%USERPROFILE%\go\bin\godoc

Slide 26

Slide 26 text

6OPSEFSFEPVUQVU w ॱং͕ෆఆͳෳ਺ߦͷग़ྗ΋&YBNQMFؔ਺Ͱऔ Γѻ͏͜ͱ͕Մೳ w ॱং͕ෆఆͳNBQ͕ඞཁͳςετͰศར func ExampleTHI_map() { for humidity := range map[float64]bool{-1: true, 50: true, 101: true} { fmt.Println(THI(20.5, humidity) ) } // Unordered output : // 65.8 5 // Na N // Na N } NBQ͔ΒͷऔΓग़͠ॱং͸ϥϯμϜ ॱংΛม͑ͯ΋ύε͢Δ l6OPSEFSFEPVUQVUzͱ͢Δ

Slide 27

Slide 27 text

ͦͷଞͷػೳ

Slide 28

Slide 28 text

HPUFTUͷ௥Ճػೳ w ϓϩϑΝΠϦϯά DQVQSP fi MF CMPDLQSP fi MF NFNQSP fi MF w $16ϓϩϑΝΠϧɺώʔϓϓϩϑΝΠϧɺ଴ͪϓϩϑΝΠϧ Λه࿥ɾදࣔՄೳ ڭՊॻઅΛࢀর w ϨʔεσΟςΫλʔ SBDF w ෳ਺ͷHPSPVUJOF͕ಉ࣌ʹҰͭͷ஋ΛಡΈॻ͖ গͳ͘ͱ΋ ͕ͭॻ͖ࠐΈ ͍ͯ͠Δ৔໘ ഉଞ੍ޚ࿙Ε Λݕ஌ͯ͠Τϥʔ ʹམͱͯ͘͠ΕΔ w ࣮ࡍʹൃੜ͠ͳ͍ͱݕ஌Ͱ͖ͳ͍ͷͰɺ໷௨͠ಉ͡ςετΛ ܁Γฦ࣮͠ߦͤ͞Δ౳ͷӡ༻ͱ૊Έ߹ΘͤΔͷ͕͓קΊ

Slide 29

Slide 29 text

UFTUJOHͷ௥Ճػೳ w ςετॲཧ༻ͷNBJOؔ਺ UFTUJOH. ˞(PͰಋೖ w ςετؔ਺͕૸Γग़͢લʹNBJOHPSPVUJOFͰ૸Β͓͖ͤͯ ͍ͨॲཧ΍ΫϦʔϯΞοϓ͍ͨ͠ॲཧ͕͋Δ৔߹ʹར༻ w αϒςετɾαϒϕϯνϚʔΫ U3VO C3VO ˞(PͰಋೖ w ϧʔϓͷதͳͲ͔Β৽ͨͳςετ΍ϕϯνϚʔΫΛ࣮ߦͰ͖Δ w ςʔϒϧۦಈܕϕϯνϚʔΫ΍֊૚ςετͳͲෳࡶͳ͜ͱΛ͠ ͍ͨͱ͖ͷΈ࢖͏ 詳 し く は t e s t i n g の g o d o c に あ る E x a m p l e コ ー ド をチ ェ ッ ク !

Slide 30

Slide 30 text

࿅श

Slide 31

Slide 31 text

࿅श໰୊ w ࿅श՝୊<ϑΥϧμ໊EBZUFTU> w ΧόϨοδ͕ʹͳΔΑ͏ͳςʔϒϧۦ ಈςετͷςετؔ਺Λ௥Ճ͍ͯͩ͘͠͞

Slide 32

Slide 32 text

ʙղઆλΠϜʙ

Slide 33

Slide 33 text

͕࣌ؒ༨ͬͨΒ w ࿅श໰୊ 1 <ϑΥϧμ໊EBZDIFY> w ·ͣ͸1ʹ͋ΔຊମίʔυIsPalindrome()ͱ 1ʹ͋Δςετίʔυͷࣸܦ͔Β w ࿅श໰୊ 1 <ϑΥϧμ໊EBZDIFY> w ͷίʔυΛίϐʔ͔ͯ͠Β࢝ΊΔͱΑ͍ w ࿅श໰୊ 1 <ϑΥϧμ໊EBZDIFY> w ·ͣ͸1ͷ5FTU4QMJU ͷࣸܦ͔Β

Slide 34

Slide 34 text

ίʔυΛॻ͖ऴ͑ͨΒ w ʮϢʔβʔ໊QSBDUJDFʯϒϥϯνͰ࡞ۀ͍ͯ͠Δ͜ͱΛ֬ೝ͍ͯͩ͘͠͞ w 74$PEFͷҰ൪ࠨԼʹදࣔ͞Ε͍ͯ·͢ w มߋΛεςʔδ͠ɺίϛοτ͍ͯͩ͘͠͞ w 74$PEFͷࠨϝχϡʔͷʮιʔε؅ཧʯ͔Βૢ࡞ w ίʔυΛ(JU)VCʹϓογϡ͍ͯͩ͘͠͞ w 74$PEFͷҰ൪ࠨԼͷϒϥϯν໊ͷ͙͢ӈʹ͋ΔΞΠίϯ͔Βૢ࡞ ্ਤ w ϓϧϦΫΤετΛ࡞੒͍ͯͩ͘͠͞ w ʮιʔε؅ཧʯͷ্෦ʹΞΠίϯ͕͋Γ·͢

Slide 35

Slide 35 text

࣍ճ༧ࠂ w ࣍ճ͸ʮσʔλϕʔεΞΫηεʯͰ͢ w ෼͔Βͳ͔ͬͨͱ͜Ζ΍࣭໰ͳͲ͸ɺࣾ಺ 4MBDLHPQIFSTνϟϯωϧʹ౤ߘ͍ͩ͘͞ w ຊ೔ͷ಺༰ΛΑΓਂ͘ཧղ͍ͨ͠ํ͸ɺڭՊॻ ͷୈষΛಡΜͰΈ͍ͯͩ͘͞