Go 언어에는 while 반복문이 없으므로 for 반복문을 (while 반복문 처럼) 쓰면 된다.

 

소스 파일명: testWhile.go

  1. // Filename: testWhile.go
  2. //
  3. // Execute: go run testWhile.go 625 1000
  4. // or
  5. // Compile: go build testWhile.go
  6. // Execute: ./testWhile 625 1000
  7. package main
  8. import (
  9.     "fmt"
  10.     "os"
  11.     "strconv"
  12. )
  13. // 사용법 표시 함수
  14. func printUsing() {
  15.     fmt.Printf("Using: testWhile [integer1] [integer2]\n");
  16.     fmt.Printf("This finds the greatest common divisor of the given two integers.\n");
  17. }
  18. // main 함수
  19. func main() {
  20.     var val1 int64
  21.     var val2 int64
  22.     var a int64
  23.     var b int64
  24.     var gcd int64
  25.     if len(os.Args) != 3 {
  26.         printUsing();
  27.         return
  28.     }
  29.     // 명령행 인자의 스트링을 가져와서
  30.     // 64비트 정수로 변환하여
  31.     // 변수 val1과 val2에 저장한다.
  32.     val1, _ = strconv.ParseInt(os.Args[1], 10, 64);
  33.     val2, _ = strconv.ParseInt(os.Args[2], 10, 64);
  34.     a = val1
  35.     if a < 0 {
  36.         a = -a
  37.     }
  38.     b = val2;
  39.     if b < 0 {
  40.         b = -b
  41.     }
  42.     if a < b { 
  43.         a, b = b, a
  44.     }
  45.     if b == 0 {
  46.         fmt.Printf("GCD(%d, %d) = %d\n", val1, val2, a);
  47.         return
  48.     }
  49.     // -------------------------------------------
  50.     // Euclidean 알고리즘의 시작 
  51.     // *  Go 언어에는 while 반복문이 없으므로 for 문을 사용한다.
  52.     for a != 0 {
  53.         b, a = a, b % a
  54.     }
  55.     // 나머지가 0이면 그 때 나눈 수(제수) b가 최대공약수(GCD)이다.
  56.     gcd = b
  57.     // 최대공약수(GCD)를 출력한다.
  58.     fmt.Printf("GCD(%d, %d) = %d\n", val1, val2, gcd);
  59. }


 



실행:

Command> go run testWhile.go
Using: testWhile [integer1] [integer2]
This finds the greatest common divisor of the given two integers.

Command> go run testWhile 200 -300
GCD(200, -300) = 100

Command>go run  testWhile 0 -300
GCD(0, -300) = 300

Command> go run testWhile 20 -125
GCD(20, -125) = 5

Command> go run testWhile 121 66
GCD(121, 66) = 11

Command> testWhile -111 -37
GCD(-111, -37) = 37


컴파일:

Command> go build testWhile.go


실행:

Command> testWhile
Using: testWhile [integer1] [integer2]
This finds the greatest common divisor of the given two integers.

Command> testWhile 200 -300
GCD(200, -300) = 100

Command> testWhile 0 -300
GCD(0, -300) = 300

Command> testWhile 20 -125
GCD(20, -125) = 5

Command> testWhile 121 66
GCD(121, 66) = 11

Command> testWhile -111 -37
GCD(-111, -37) = 37

 

Posted by Scripter
,

소스 파일명: testIf.go

  1. // Filename: testIf.go
  2. //
  3. // Execute: go run testIf.go -1.5
  4. // or
  5. // Compile: go build testIf.go
  6. // Execute: ./testIf -1.5
  7. package main
  8. import (
  9.     "fmt"
  10.     "os"
  11.     "strconv"
  12. )
  13. // 사용법 표시 함수
  14. func printUsing() {
  15.     fmt.Printf("Using: testIf [number]\n");
  16.     fmt.Printf("This determines whether the number is positive or not.\n");
  17. }
  18. // main 함수
  19. func main() {
  20.     var val = 0.0
  21.     if (len(os.Args) != 2) {
  22.         printUsing();
  23.         return
  24.     }
  25.     // 명령행 인자의 스트링을 가져와서
  26.     // 배정밀도 부동소수점수로 변환하여
  27.     // 변수 val에 저장한다.
  28.     val, _ = strconv.ParseFloat(os.Args[1], 64);
  29.     // 변수 val에 저장된 값이 양수인지 음수인지 0인지를
  30.     // 판단하는 if...else... 조건문
  31.     if val > 0.0 {
  32.         fmt.Printf("%g is a positive number.\n", val);
  33.     } else if val < 0.0 {
  34.         fmt.Printf("%g is a negative number.\n", val);
  35.     } else  {
  36.         fmt.Printf("%g is zero.\n", val);
  37.     }
  38. }



실행> go run testIf.go
Using: testIf [number]
This determines whether the number is positive or not.

실행> go run testIf.go 1.234
1.234 is a positive number.

실행> go run testIf.go -1.234
-1.234 is a negative number.

실행> go run testIf.go 0
0 is zero.

또는

 


컴파일> go build testIf.go

실행> testIf
Using: testIf [number]
This determines whether the number is positive or not.

실행> testIf 1.234
1.234 is a positive number.

실행> testIf -1.234
-1.234 is a negative number.

실행> testIf 0
0 is zero.


Posted by Scripter
,

Go 소스 파일에 한글이 포함되어 있으면  UTF8 인코딩으로 저장해야 한다.

     소스 파일명: testArguments.go

  1. // Filename: testArguments.go
  2. //
  3. // Execute: go run testArguments.go 1 2 3
  4. // or
  5. // Compile: go build testArguments.go
  6. // Execute: ./testArguments 1 2 3
  7. package main
  8. import (
  9.     "fmt"             // fmt.Printf 함수 사용을 위해
  10.     "os"             // os.Args 사용을 위해
  11.     "strconv"      // strconv.ParseFloat 함수 사용을 위해
  12. )
  13. func main() {
  14.     var sum = 0.0;
  15.     var y = 0.0
  16.     for i, x := range os.Args {
  17.         if i > 0 {
  18.             // 스트링을 배정밀도 부동소수점수로 변환하여 변수 sum에 누적
  19.             y, _ = strconv.ParseFloat(x, 64)
  20.             // fmt.Printf("y = %f\n", y)
  21.             sum += y
  22.         }
  23.     }
  24.     // 명령행 인자(command-line argument) 개수 출력
  25.     fmt.Printf("Count of arguments: %d\n", len(os.Args));
  26.     // 부동소수점수 값을 %g로 출력
  27.     fmt.Printf("The sum of arguments is %g\n", sum);
  28. }



실행> go run testArguments.go 1 2 3 4
Count of arguments: 5
The sum of arguments is 10

실행> go run testArguments.go 1 2 3 4.2
Count of arguments: 5
The sum of arguments is 10.2

 

또는

 

컴파일> go build testArguments.go

실행> testArguments 1 2 3 4
Count of arguments: 5
The sum of arguments is 10

실행> testArguments 1 2 3 4.2
Count of arguments: 5
The sum of arguments is 10.2


 

Posted by Scripter
,

 

 

// Filename: HelloWorld.go
//
// Execute: go run HelloWorld.go
// or
// Compile: go build HelloWorld.go
// Execute: ./HelloWorld

package main

import "fmt"

func main() {
    fmt.Printf("Hello, world!\n")
}

 

 

Posted by Scripter
,