본문 바로가기

GO lang9

조건문 if 조건문 보통 알고 있는 if문의 문법과 거의 동일하다. if 조건 { } 의 형식이다. if else 형식도 기존의 다른 언어 들과 동일하다. 다만 조건문의 Scope는 python과는 다르게 Brace로 구별한다. else if 문도 지원한다. c++, java, javascript라 다른 점은 조건을 쓸때 괄호로 감싸지 않는다. package main import ( "fmt" "math/rand" ) func main() { randNumber := rand.Intn(20) if randNumber > 10 { fmt.Println("this number over 10") } else if randNumber > 5 { fmt.Println("this number over 5") } else { f.. 2020. 12. 18.
반복문 GO lang에서의 반복문 특별히 설명할 것이 많이 없다. 단순하게 for문 하나뿐이 없다. 다만, For문하지 지만, 다양한 형태를 제공해 주기 때문에 특별하게 다른 예약어는 없이 그냥 for문하나로 통일해 버렷다. 일반적인 For문 for i := 0; i < 10; i++ { sum += i } while 문 형태 sum := 1 for sum < 1000 { sum += sum } foreach문 형태 아직 배열 초기화나 배열에 대해서 살펴 본것이 아니기 때문에 그냥 형태만 살펴 보면 된다. animals := []string{"dog", "cat", "hedgehog"} for _, animal := range animals { fmt.Println("My animal is:", animal) .. 2020. 12. 14.
Go lang #2 변수들 변수는? 처음 Go lang에서 느끼는 변수는 결국 java, c#, javascript 같은 느낌을 받게 된다. 기본적으로 var라는 것을 사용한다. 타입을 명시하는 것이 가능하다. 타입없이도, 선언 동시 할당을 하면, 컴파일러가 알아서 변수의 타입을 추론하여 만들어 준다. var typerand int = rand.Intn(100) 와 같은 코드는 rand.intn이 int값을 생성시키는 변수 이기 때문에 typerand값는 int로 선언하였다. typerand := rand.intn(100) 이라는 방식도 가능한다. :=는 즉시 할당하겟다는 의미로, 선언과 할당을 동시에 하기 때문에 전혀 문제 없이 사용이 가능하다. 변수의 타입 종류는 아래사이트에서 확인할수있다. 예제로 배우는 Go 프로그래밍 - .. 2020. 12. 9.
GO lang 설치와 실행 Go lang 설치 Download and install - The Go Programming Language (golang.org) Download and install - The Go Programming Language Download and install Download and install Go quickly with the steps described here. For other content on installing, you might be interested in: 1. Go download. Click the button below to download the Go installer. Download Go Don't see your operating syste golang.org 공식 홈.. 2020. 12. 2.