본문 바로가기

groovy

[Groovy] What is Groovy, Getting Started Groovy!

반응형

내가 Groovy를 익혀야 겠다고 생각한 이유는 Gradle 과 Spock 때문이었다. 

언어를 시작 할 때 주로 책을 보고 익히는 편인데 원서를 제외(영어가 짧아..) 하고 국내 책이 거의 없는 관계로 정리 차원에 블로깅을 하기로 결심했다.


# What the Groovy #

먼저 그루비(Groovy)는 자바에 파이썬, 루비, 스몰토드 등의 특징을 더한 동적 객체 지향 프로그래밍 언어이다.

# History #

2002년 영국 출신의 프로그래머인 제임스 스트라칸에 의해 창시되었다고 한다.

그의 말에 따르면 그루비의 가장 중요한 점이 타입이 유연 간결한 동적인 프로그래밍 언어라고 한다.

현재 (2014년 6월) 기준으로 2.3.2 버전이 나와있다.

# Feature #

특징은 

> 자바 가상 머신에서 작동하는 동적 타이핑 프로그래밍 언어

> 자바의 강점 위에 파이썬, 루비, 스몰토크 등의 프로그래밍 언어에 영향을 받은 특장점을 더함

> 자바 프로그래머들이 많은 학습을 하지 않고도 최신 프로그래밍 기법을 사용할 수 있음.

> 도메인 전문 언어와 단순화된 문법을 지원, 코드가 읽기 쉽고 유지 보수가 편해짐.

요약하자면 자바와 유사하며, 자바가 필요로 하는 모든 요소를 요구하지 않아 좀 더 편하고, 컴팩트하다 정로도 요약할 수 있겠다.


좀 더 자세한 내용은 아래 사이트를 통해 확인 할 수 있다. 

http://groovy.codehaus.org




막연하게 시작하기 위해서 자료를 뒤져보다가 고맙게도 사이트에서 한국어 지원 튜토리얼이 있어서 따라해 보게 되었다.


http://groovy.codehaus.org/Korean+Tutorial+1+-+Getting+started

가장 먼저 JDK(JDK 1.8) 와 Groovy(groovy 2.3.1) 설치를 한다. 설치 방법에 대해서는 따로 언급하지 않겠다.

나는 MacBook을 사용하기에 Homebrew를 통해 쉽게 설치 할 수 있었다.


이제 설치도 다 끝났는데 어떤 것 부터 시작을 해야하나 하는 고민에 

console 창에 "groovyConsole" 이라는 명령어를 입력하면 

친절하게도 GroovyConsole 이란 Editor 가 열리고 간단한 입력으로 쉽게 편집 / 수행(Run : command + r)을 해 볼 수가 있었다.




// [예제1 - Hello World]
println "Hello, World!"

// [예제2 - 변수]
x = 1
println x

x = new java.util.Date()
println x

x = -3.1499392
println x

x = false
println x

x = "Hi"
println x

// [예제3 - List와 Map]
// List : Java의 배열과 비슷하게 처리한다.
myList = [1776, -1, 33, 99, 0, 928734928763]
println myList[0]
println myList.size()
// Map : 서로 다른 타입 등록이 가능하다.
scores = [ "Brett":100, "Pete":"Did not finish", "Andrew":86.87934 ]
// Map의 값에 접근 방법
println scores["Pete"] 
println scores.Pete
// Map에 자료 추가
scores["Pete"] = 3
println scores["Pete"]
// 빈 Map, List 
emptyMap = [:]
emptyList = []

// [예제4 - 조건적 실행]
amPM = Calendar.getInstance().get(Calendar.AM_PM)
if (amPM == Calendar.AM)
{
	println("Good morning")
} else {
	println("Good evening")
}

amPM = Calendar.getInstance().get(Calendar.AM_PM)
if (amPM == Calendar.AM)
{
	println("Have another cup of coffee.")
}

// [예제5 - Boolean 표현식]
myBooleanVariable = true

titanicBoxOffice = 1234600000
titanicDirector = "James Cameron"

trueLiesBoxOffice = 219000000
trueLiesDirector = "James Cameron"

returnOfTheKingBoxOffice = 752200000
returnOfTheKingDirector = "Peter Jackson"

theTwoTowersBoxOffice = 581200000
theTwoTowersDirector = "PeterJackson"

titanicBoxOffice > returnOfTheKingBoxOffice  // evaluates to true
titanicBoxOffice >= returnOfTheKingBoxOffice // evaluates to true
titanicBoxOffice >= titanicBoxOffice         // evaulates to true
titanicBoxOffice > titanicBoxOffice          // evaulates to false
titanicBoxOffice + trueLiesBoxOffice < returnOfTheKingBoxOffice + theTwoTowersBoxOffice  // evaluates to false

titanicDirector > returnOfTheKingDirector    // evaluates to false, because "J" is before "P"
titanicDirector < returnOfTheKingDirector    // evaluates to true
titanicDirector >= "James Cameron"           // evaluates to true


// [예제6 - 조건문과 Boolean 표현식]
titanicBoxOffice = 1234600000
titanicDirector = "James Cameron"

trueLiesBoxOffice = 219000000
trueLiesDirector = "James Cameron"

returnOfTheKingBoxOffice = 752200000
returnOfTheKingDirector = "Peter Jackson"

theTwoTowersBoxOffice = 581200000
theTwoTowersDirector = "PeterJackson"

if (titanicBoxOffice + trueLiesBoxOffice > returnOfTheKingBoxOffice + theTwoTowersBoxOffice)
{
	println(titanicDirector + " is a better director than " + returnOfTheKingDirector)
}

suvMap = ["Acura MDX":"\$36,700", "Ford Explorer":"\$26,845"]
if (suvMap["Hummer H3"] != null)
{
 	println("A Hummer H3 will set you back "+suvMap["Hummer H3"]);
}


반응형