以ä¸ã®ãµã¤ããåèã«ãã£ã¦ã¿ã¾ããã
Go ã¯å ¬å¼ããã¤ã³ã¹ãã¼ã«ã
VS Code ãå ¬å¼ããã¤ã³ã¹ãã¼ã«ã
Go ã®ã¤ã³ã¹ãã¼ã«ã¯ c:\go
ã«ãã¾ãããã¤ã³ã¹ãã¼ã«å¾ç°å¢å¤æ°ãè¦ã㨠GOPATH ç°å¢å¤æ°ã追å ããã¦ãã¦ã¦ã¼ã¶ã¼ãã£ã¬ã¯ããªã®ä¸ã® go ãè¨å®ããã¦ãã¾ããã
VS Code ã« Go ã®æ¡å¼µæ©è½ãå ¥ãã¾ããã£ã¦ããããããªãã·ã£ã¼ããã¤ã¯ãã½ããï¼ï¼ç¥ãããã£ãâ¦ã å°ãåã® Go ã®ç°å¢æ§ç¯ã®è¨äºãè¦ãéã Microsoft ã§ã¯ãªããããªãã·ã£ã¼ã®ã¹ã¯ã·ã§ã¨ããè¦åããããã®ã§æè¿å¤ãã£ããã§ãããï¼
ãã®æ®µé㧠.go
ã®ãã¡ã¤ã«ã®ãããã©ã«ããéãã¦ãã¡ã¤ã«ãé©å½ã«éãã¨ä»¥ä¸ã®ãã¼ã«ã®ã¤ã³ã¹ãã¼ã«ããããã¨ãã確èªãåºãã®ã§ Install All ãã¾ããã
Installing 12 tools at C:\Users\kaota\go\bin gocode gopkgs go-outline go-symbols guru gorename dlv gocode-gomod godef godef-gomod goreturns golint
ããã§ã¤ã³ã¹ãã¼ã«ããªãã¦ã Go: Install/Update tools
ã§åããã¨ãåºæ¥ã¾ãã
ç§ã¯ããã®ã㨠Go: Install/Update tools
ãå®è¡ãã¦å
¨ãã¼ã«ã追å ãã¦ããã¾ããã
å®è¡ãã¦ã¿ãã
GOHOME ã®ä¸ã® src ãã©ã«ãã« hello_world ã¨ãããã©ã«ããä½ã£ã¦ Visual Studio Code ã§éãã¾ãã main.go ã¨ããååã§ãã¡ã¤ã«ãéãã¦ä»¥ä¸ã®ãããªã³ã¼ããæ¸ãã¾ãã
package main import ( "fmt" ) func main() { fmt.Print("Hello world") }
æ¸ãã¦ãéä¸ã«è£éå ·åãè¦ã¦ã¿ãã®ã§ããçµæ§åªç§ããã§ããä¸ã®ç»åã®ã㨠Enter ãã¼ãæ¼ã㨠import ãèªåæ¿å ¥ããã¾ããã
Ctrl + Shift + B ãæ¼ãã¦ãã¿ã¹ã¯ãæ§æããã¦ããªãã¨åºãã®ã§ããã³ãã¬ã¼ããã task.json ãçæãé¸æã㦠Others ãé¸ãã§åºã¦ãã tasks.json ã以ä¸ã®ããã«ãã¾ããã
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "run", "type": "shell", "command": "go", "args": [ "run", "${file}" ] } ] }
ããä¸åº¦ main.go ã«è¡ã£ã¦ Ctrl + Shift + B ãããã¨ããã©ã«ãã®ãã«ãã¿ã¹ã¯ãé¸æããããã«è¨ãããã®ã§ run ãé¸ã³ã¾ããããã¦ãããä¸åº¦ main.go ã«æ»ã£ã¦ Ctrl + Shift + B ãããã¨ã¡ããã¨å®è¡ããã¾ããã
Hello world
ãããã°ã®ç»é¢ãåºã㦠F5 ãæ¼ãã¨ãããã°ãå§ã¾ãã¾ãããã ãç§ã®ã¨ããã ã¨å°ãæéããããã¾ããã
åä½ãã¹ãã¯ï¼
testing ã¨ããããã±ã¼ã¸ã使ãã¿ããã§ããåèã«ãããã¼ã¸ã
ãã¡ã¤ã«åã®çµãã㯠_test.go ã§ãã¹ããããé¢æ°ã¯ Test ã§å§ã¾ã£ã¦å¼æ°ã« t *testing.T
ãåãåã£ã¦è²ã
ããã¿ããã§ããã
ã¾ã ããã®ãã¼ã¸ãæµãèªã¿ããã ãã§ããä»ã®è¨èªã§ãããã AssertXXX çãªã¡ã½ããã¯æ¨æºã§ã¯ãªãããã
ã¨ãããããããããã®ãæ¸ãã¦ã¿ã¾ããã
package main import ( "testing" ) func TestSuccess(t *testing.T) { var x = 1 var y = 2 var expected = 3 var actual = x + y if x+y != 3 { t.Errorf("expected: %v, but actual: %v", expected, actual) } } func TestFailed(t *testing.T) { var x = 1 var y = 2000 var expected = 3 var actual = x + y if x+y != 3 { t.Errorf("expected: %v, but actual: %v", expected, actual) } }
ããã¨ãVSCode ã®ç»é¢ããããªæãã«ãªãã¾ããã
CodeLens ã«ãã¹ãèµ·åã®ãªã³ã¯ãâ¦ãæ¼ãã¦ã¿ãã¨å®è¡ãã¦ããã¾ããã
Running tool: C:\Go\bin\go.exe test -timeout 30s hello_world -coverprofile=C:\Users\kaota\AppData\Local\Temp\vscode-goPdh5s3\go-code-cover --- FAIL: TestFailed (0.00s) c:\Users\kaota\go\src\hello_world\main_test.go:25: expected: 3, but actual: 2001 FAIL coverage: 0.0% of statements FAIL hello_world 0.254s Error: Tests failed.
åããã«ãã¬ãã¸ãã²ã£ããã¨åºãã¦ããã¦ãã¿ããã§ãããå¯è¦åããããªã
debug test ãæ¼ãã¨ã¡ããã¨ãããã°ãã¦ããã¾ããã
ãããã°ç°å¢ãããã¡ãã®ãµã¤ããåèã« json ææ¸ãããªãã¨æã£ã¦ã¾ãããä»ã¯ç¹ã«å¿ è¦ãªãããã§ãã
ã¾ã¨ã
ã¤ã³ã¹ãã¼ã©ã¼å©ãã¦æ¡å¼µæ©è½å ¥ãããããã¨ã¯å¾ã£ã¦ãã ãã§ããã Ctrl + Shift + B ã§è»½ãå®è¡ããããªã¨æã£ããæã§å°ã json æ¸ããããã§ããããã
Ctrl + Shift + P ãæ¼ã㦠go: ã§çµãè¾¼ãã§ã¿ãã¨ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®ã¹ã¿ããä½ã£ãããã¹ããçæãããã¨ãã£ãæ©è½ãããã¿ããã§ãããããç´ æµããã§ãããå¾ã§è§¦ã£ã¦ã¿ããã
ã¨ãããã¨ã§çµæ§å¿«é©ã« Windows ã§ã試ãåºæ¥ãããªã®ã§è§¦ã£ã¦ãã£ã¦ã¿ããã¨æãã¾ãã