GO์–ธ์–ด

[Go] ํŒจํ‚ค์ง€ ๊ธฐ์ดˆ, ๋ฐ์ดํ„ฐ ํƒ€์ž…

HHRR 2023. 1. 20. 09:45

ํŒจํ‚ค์ง€ ๊ธฐ๋ณธโญโญโญ

package main

//์„ ์–ธ ๋ฐฉ๋ฒ•
import (
	"fmt"
	"os"
)

func main() {
	var name string
	fmt.Println("์ด๋ฆ„์€?: ")
	fmt.Scanf("%s", &name)

	fmt.Fprintf(os.Stdout, "Hi! %s\n", name) //ํ„ฐ๋ฏธ๋„์—์„œ ์ง„ํ–‰
	// cd, go run .\package1.go

}

 

- ํŒจํ‚ค์ง€ : ์ฝ”๋“œ ๊ตฌ์กฐํ™”(๋ชจ๋“ˆ) ๋ฐ ์žฌ์‚ฌ์šฉ
- ์‘์ง‘๋„, ๊ฒฐํ•ฉ๋„
- Go : ํŒจํ‚ค์ง€ ๋‹จ์œ„์˜ ๋…๋ฆฝ์ ์ด๊ณ  ์ž‘์€ ๋‹จ์œ„๋กœ ๊ฐœ๋ฐœ -> ์ž‘์€ ํŒจํ‚ค์ง€๋ฅผ ๊ฒฐํ•ฉํ•ด์„œ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑ ํ•  ๊ฒƒ์„ ๊ถŒ๊ณ 
- ํŒจํ‚ค์ง€ ์ด๋ฆ„ = ๋””๋ ‰ํ† ๋ฆฌ ์ด๋ฆ„
- ๊ฐ™์€ ํŒจํ‚ค์ง€ ๋‚ด -> ์†Œ์ŠคํŒŒ์ผ๋“ค์€ ๋””๋ ‰ํ† ๋ฆฌ๋ช…์„ ํŒจํ‚ค์ง€ ๋ช…์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
- ๋„ค์ด๋ฐ ๊ทœ์น™ : ์†Œ๋ฌธ์ž  private ํŒจํ‚ค์ง€ ์™ธ๋ถ€ ์ ‘๊ทผ ๋ถˆ๊ฐ€(๋‚ด๋ถ€๋งŒ ๊ฐ€๋Šฅ), ๋Œ€๋ฌธ์ž : public ํŒจํ‚ค์ง€ ์™ธ๋ถ€์—์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅ

- Go : main ํŒจํ‚ค์ง€๋Š” ํŠน๋ณ„ํ•˜๊ฒŒ ์ธ์‹ -> ์ปดํŒŒ์ผ๋Ÿฌ ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌx, ํ”„๋กœ๊ทธ๋žจ์˜ ์‹œ์ž‘์  start point!

 


  • ๋‚ด๊ฐ€ ๋งŒ๋“  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ด์šฉ
    • section4 / lib / lib.go ํŒŒ์ผ
// ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ ‘๊ทผ์ œ์–ด(1)
package lib

import "fmt"

func init() {
	fmt.Println("lib Package > init start!")
}

//num ์ฒดํฌ (์ž…๋ ฅ๊ฐ’ 10๋ณด๋‹ค ํฌ๋ฉด true, ์ด์™ธ๋Š” false)
func CheckNum(c int32) bool {
	return c > 10
}

- package lib ์œผ๋กœ ์ƒ์„ฑํ›„ num ์ฒดํฌ ํ•จ์ˆ˜ ์ž‘์„ฑ

  • section4 / package2.go ํŒŒ์ผ
package main

//go path๊ฐ€ goproject๋กœ ์ง€์ •
import (
	"fmt"
	checkUp "section4/lib" //์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
    _ "section4/lib2"
)

func main() {
	fmt.Println("10๋ณด๋‹ค ํฐ ์ˆ˜? : ", lib.CheckNum(4)) //๋Œ€๋ฌธ์ž์—ฌ์•ผ ์ ‘๊ทผ๊ฐ€๋Šฅ public
}

- main์—์„œ import๋กœ ๋‚ด๊ฐ€ ๋งŒ๋“  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ถˆ๋Ÿฌ์™€์„œ lib.CheckNum์œผ๋กœ ํ•จ์ˆ˜ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

- checkUp "section4/lib" : Alias ๋ณ„์นญ์‚ฌ์šฉ

- _ "section4/lib2" : ๋นˆ ์‹๋ณ„์ž, ์ง€๊ธˆ ์‚ฌ์šฉํ•˜์ง„ ์•Š์ง€๋งŒ ๋‚˜์ค‘์— ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ผ๋Š” ๋œป


 
  • ์ดˆ๊ธฐํ™”
//go ์ดˆ๊ธฐํ™” ํ•จ์ˆ˜(1)

package main

import (
	"fmt"
)

func init() {
	fmt.Println("Init Method Start!")
}

func main() {
	fmt.Println("Main Method Start!")
}

- init : ํŒจํ‚ค์ง€ ๋กœ๋“œ์‹œ์— ๊ฐ€์žฅ ๋จผ์ € ํ˜ธ์ถœ, ํ•œ ํŒŒ์ผ์— ์—ฌ๋Ÿฌ๊ฐœ ์žˆ์–ด๋„๋จ
- ๊ฐ€์žฅ ๋จผ์ € ์ดˆ๊ธฐํ™”๋˜๋Š” ์ž‘์—… ์ž‘์„ฑ ์‹œ ์œ ์šฉํ•˜๋‹ค

- importํ•œ ์™ธ๋ถ€ ํŒจํ‚ค์ง€ ์‚ฌ์šฉ์‹œ์— ๊ทธ ํŒจํ‚ค์ง€์— ์žˆ๋Š” ์ดˆ๊ธฐํ™”๋‚˜ ์ž‘์—… ๋จผ์ € ์‹คํ–‰ํ›„ main์œผ๋กœ ๋„˜์–ด์˜ด 


๋ฐ์ดํ„ฐ ํƒ€์ž…

  • bool
// ๋ฐ์ดํ„ฐ ํƒ€์ž… : Bool(1)
package main

import "fmt"

func main() {
	//์˜ˆ์ œ1
	var b1 bool = true
	var b2 bool = false
	b3 := true
	fmt.Println("ex1 : ", b1)
	fmt.Println("ex2 : ", b2)
	fmt.Println("ex3 : ", b3)

	fmt.Println("ex4 : ", b3 == b3)
	fmt.Println("ex5 : ", b1 && b3)
	fmt.Println("ex6 : ", b1 || b2)
	fmt.Println("ex7 : ", !b1)
    
    
    	//์˜ˆ์ œ1 ๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž
	fmt.Println("ex : ", true && true)
	fmt.Println("ex : ", true && false)
	fmt.Println("ex : ", false && false)
	fmt.Println("ex : ", true || true)
	fmt.Println("ex : ", true || false)
	fmt.Println("ex : ", false || false)
	fmt.Println("ex : ", !true)
	fmt.Println("ex : ", !false)

	//์˜ˆ์ œ2 ๋น„๊ต์—ฐ์‚ฐ์ž
	num1 := 15
	num2 := 37

	fmt.Println("ex : ", num1 < num2)
	fmt.Println("ex : ", num1 > num2)
	fmt.Println("ex : ", num1 == num2)
	fmt.Println("ex : ", num1 != num2)

}

- Bool(Boolean) : ์ฐธ๊ณผ ๊ฑฐ์ง“
- ์กฐ๊ฑด๋ถ€ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž๋ž‘ ์ฃผ๋กœ ์‚ฌ์šฉ : !, ||(or), &&(and)
- ์•”๋ฌต์  ํ˜•๋ณ€ํ™˜ ์•ˆ๋จ : 0, Nil -> False ๋ณ€ํ™˜ ์—†์Œ


  • Numeric, Go์—์„œ ๋‹ค์–‘ํ•˜๊ฒŒ ์‚ฌ์šฉํ•จ(๋ณต์†Œ์ˆ˜๋„ ์žˆ์Œ)
//๋ฐ์ดํ„ฐ ํƒ€์ž… : Numeric(1)

package main

import "fmt"

func main() {

	var num1 int = 17
	var num2 int = -68
	var num3 int = 0631       //8์ง„์ˆ˜
	var num4 int = 0x32fa2c75 //16์ง„์ˆ˜

	fmt.Println("ex1 : ", num1)
	fmt.Println("ex1 : ", num2)
	fmt.Println("ex1 : ", num3)
	fmt.Println("ex1 : ", num4)

}

- ๋ฐ์ดํ„ฐ ํƒ€์ž… : ์ˆซ์žํ˜•
- ์ •์ˆ˜, ์‹ค์ˆ˜, ๋ณต์†Œ์ˆ˜
- 32bit, 64bit, unsigned(์–‘์ˆ˜)
- ์ •์ˆ˜ : 8์ง„์ˆ˜(0), 16์ง„์ˆ˜(0x), 10์ง„์ˆ˜

//๋ฐ์ดํ„ฐ ํƒ€์ž… : Numeric(2) go์—์„œ ๋‹ค์–‘ํ•˜๊ฒŒ ์‚ฌ์šฉํ•จ

package main

import "fmt"

func main() {
	//๋ฐ์ดํ„ฐ ํƒ€์ž… : ์ˆซ์žํ˜•
	//์ •์ˆ˜ํ˜• ๋ฌธ์ž ์ถœ๋ ฅ

	//์˜ˆ์ œ1
	//์•„์Šคํ‚ค(์˜๋ฌธ)
	//byte 8bit
	var char1 byte = 72
	var char2 byte = 0110
	var char3 byte = 0x48

	//์˜ˆ์ œ2
	//์œ ๋‹ˆ์ฝ”๋“œ(ํ•œ๊ธ€)
	//rune 32bit
	var char4 rune = 50556   //์œ ๋‹ˆ์ฝ”๋“œ
	var char5 rune = 0142574 //44032(8์ง„์ˆ˜)
	var char6 rune = 0xC57C  //44032(16์ง„์ˆ˜)

	//%c ๋ฌธ์ž, %d 10์ง„์ˆ˜, %o 8์ง„์ˆ˜, %x 16์ง„์ˆ˜
	fmt.Printf("%c %c %c\n", char1, char2, char3)
	fmt.Printf("%d %d %d\n", char1, char2, char3)
	fmt.Printf("%d %o %x\n", char1, char2, char3)

	fmt.Printf("%c %c %c\n", char4, char5, char6)
	fmt.Printf("%d %d %d\n", char4, char5, char6)
	fmt.Printf("%d %o %x\n", char4, char5, char6)

}
  • ๋ถ€๋™์†Œ์ˆ˜์  ์˜ค์ฐจ ์ฃผ์˜
//๋ฐ์ดํ„ฐ ํƒ€์ž… : Numeric(3)

package main

import "fmt"

func main() {
	//์‹ค์ˆ˜(๋ถ€๋™์†Œ์ˆ˜์ )
	//floot32(7์ž๋ฆฌ), floot64(15์ž๋ฆฌ)

	//์˜ˆ์ œ1
	var num1 float32 = 0.14
	var num2 float32 = .75647
	var num3 float32 = 442.0378373
	var num4 float32 = 10.0

	//์ง€์ˆ˜ ํ‘œ๊ธฐ๋ฒ•
	var num5 float32 = 14e6
	var num6 float32 = .156875e+3
	var num7 float32 = 5.32521e-10

	fmt.Println("ex1 : ", num1)
	fmt.Println("ex1 : ", num2)
	fmt.Println("ex1 : ", num3)
	fmt.Println("ex1 : ", num4)
	fmt.Println("ex1 : ", num4-0.1)
	fmt.Println("ex1 : ", float32(num4-0.1)) //ํ˜•๋ณ€ํ™˜
	fmt.Println("ex1 : ", float64(num4-0.1)) //๋ฒ”์œ„ ํญ ๋„“์–ด์ง€๋ฉด ๋ฏธ์„ธํ•˜๊ฒŒ ๊ฐ’ ๋‹ฌ๋ผ์ง. ๋ถ€๋™์†Œ์ˆ˜์  ์˜ค์ฐจ ์ผ์–ด๋‚จ / ์€ํ–‰๊ถŒ๋“ฑ ์ฃผ์˜!!
	fmt.Println("ex1 : ", num5)
	fmt.Println("ex1 : ", num6)
	fmt.Println("ex1 : ", num7)

}

- ๊ฐ’ ๊ณ„์‚ฐํ• ๋•Œ ํ˜•๋ณ€ํ™˜์œผ๋กœ ํ˜• ๋งž์ถฐ์คŒ

- ์ฃผ์˜) ๋ฒ”์œ„ ํญ(ex.float64) ๋„“์–ด์ง€๋ฉด ๋ฏธ์„ธํ•˜๊ฒŒ ๊ฐ’ ๋‹ฌ๋ผ์ง -> ๋ถ€๋™์†Œ์ˆ˜์  ์˜ค์ฐจ ์ผ์–ด๋‚จ.