๋ฐํ์(Runtime) visual code์์ ๋ธ๋ผ์ฐ์ ์คํ ๋จ์ถํค: Alt+B Go runtime์ ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ, ๊ฐ๋น์ง ์์ง, ๋์์ฑ์ ํฌํจํ์ฌ Go ํ๋ก๊ทธ๋ฆผ์ ์คํ์ ๊ด๋ฆฌํ๋ ์ญํ ์ ํฉ๋๋ค. ์ด ๋ฌธ์์์๋ Go runtime์ ์์ธํ ์ดํด๋ณด๊ณ ์ํคํ
์ด, ํน์ฑ๊ณผ ์ฅ์ ์ ์ดํด๋ด
๋๋ค. Go Runtime Architecture Go runtime์ ๋ชจ๋์์ด๊ณ ์ ์ฐํ๊ฒ ์ค๊ณ๋์์ผ๋ฉฐ ๊ฐ๋ฐ์๊ฐ ํน์ ์๊ตฌ์ฌํญ์ ๋ฐ๋ผ ๋์์ ์ฌ์ฉ์ ์ ์ํ ์ ์๋ ๊ณ์ธต์ ์ํคํ
์ณ๋ฅผ ๊ฐ์ถ๊ณ ์์ต๋๋ค. ๋ฐํ์์ ์ค์ผ์ค๋ฌ(schedualer), ๊ฐ๋น์ง ์์ง๊ธฐ(garbage collector), ๋ฉ๋ชจ๋ฆฌ ํ ๋น์(memory alllocator) ๋ฐ ์คํ๊ด๋ฆฌ(stack management)๋ฅผ ํฌํจํ ์ด๋ ค ํต์ฌ ๊ตฌ์ฑ ์์๋ก ๊ตฌ์ฑ๋ฉ๋๋ค. Schedualer Go ๋ฐํ์์ ํต์ฌ์ ๊ณ ๋ฃจํด์ ์คํ์ ๊ด๋ฆฌํ๋ ์ค์ผ์ค๋ฌ์
๋๋ค. ๊ณ ๋ฃจํด์ ํจ์จ์ ์ธ ๋์์ฑ์ ๊ฐ๋ฅํ๊ฒ ํ๋ ๊ฐ๋ฒผ์ด ์ค๋ ๋์
๋๋ค. ์ค์ผ์ค๋ฌ๋ ์ฌ์ฉ ๊ฐ๋ฅํ ์ค๋ ๋์ ๊ณ ๋ฃจํด์ ๋ถ์ฐํ๊ณ , ์ค๋ ๋ ๋ก์ปฌ ์คํ ๋ฆฌ์ง๋ฅผ ๊ด๋ฆฌํ๊ณ , I/O ์์
์ ์กฐ์ ํ๋ ์ญํ ์ ํฉ๋๋ค. thread(์ค๋ ๋): ํ๋ก๊ทธ๋จ ๋ด์์ ์คํ๋๋ ํ๋ฆ์ ๋จ์๋ก ๋์์ ์ฌ๋ฌ ์์
์ด๋ ํ๋ก๊ทธ๋จ์ ์คํํ๋ ๊ฒ์
๋๋ค. ์ฆ, ์ฝ๋๋ฅผ ์คํํ ์ ์๋ ๊ฐ ๋จ์๋ฅผ ์ค๋ ๋๋ผ๊ณ ํฉ๋๋ค. ๊ณ ๋ฃจํด(goroutine): Go ์ธ์ด๋ก ๋์์ ์คํ๋๋ ๋ชจ๋ ํ๋์ ์๋ฏธํฉ๋๋ค. ๊ณ ๋ฃจํด์ ๋ง๋๋ ๋น์ฉ์ ์ค๋ ๋์ ๋นํด ๋งค์ฐ ์ ๊ธฐ ๋๋ฌธ์ ๊ฒฝ๋ ์ค๋ ๋๋ผ๊ณ ํฉ๋๋ค. ๋ชจ๋ ํ๋ก๊ทธ๋จ์ ์ ์ด๋ ํ๋์ main() ํจ์๋ผ๋ ๊ณ ๋ฃจํด์ ํฌํจํ๊ณ ๊ณ ๋ฃจํด์ ํญ์ ๋ฐฑ๊ทธ๋ผ์ด๋์์ ์๋ํฉ๋๋ค. ๋ฉ์ธํจ์๊ฐ ์ข
๋ฃ๋๋ฉด ๋ชจ๋ ๊ณ ๋ฃจํด์ ์ข
๋ฃ๋ฉ๋๋ค. ๊ทธ๋ฌ๋ฏ๋ก ๊ณ ๋ฃจํด๋ณด๋ค main์ด ๋จผ์ ์ข
๋ฃ๋๋ ๊ฒ์ ๋ฐฉ์งํด์ผ ํฉ๋๋ค. Go ์ค์ผ์ค๋ฌ๋ ๋งค์ฐ ํจ์จ์ ์ด๊ณ ํ์ฅ ๊ฐ๋ฅํ๋๋ก ์ค๊ณ๋์ด ๋ง์ ์์ ๋์ ๊ณ ๋ฃจํด์ ์์ฝ๊ฒ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค. ์ค๋ ๋ ๊ฐ์ ๋ถํ๋ฅผ ๋ถ์ฐํ์ฌ ๊ฒฝํฉ์ ์ต์ํํ๊ณ ์ฑ๋ฅ์ ๊ฐ์ ํ๋ ์์
ํ์น๊ธฐ ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํฉ๋๋ค...
Smart Cast
In Kotlin, data types have the following meanings:
1) Define the size in which the object is stored
2) Whether objects are replaced or not. That is, they must have the same data type to execute.
2) Whether objects are replaced or not. That is, they must have the same data type to execute.
For example, the operation of Int type and Double type is not performed.
val a: Int = 17
val b: Double= a
println(b)
error: type mismatch: inferred type is Int but Double was expected
val b: Double= a
val b: Double= a
println(b)
error: type mismatch: inferred type is Int but Double was expected
val b: Double= a
The assignment of a to object b is not done because the data types of objects a and b are different.
When using a data type to which a smart cast is applied, the data type is specified according to the assigned value.
The Number type is a data type to which a representative smart cast for numbers is applied.
Therefore, if the Number type is applied in the above case, it is as follows.
val a: Int = 17
val b: Number=a
println(b)
17
val b: Number=a
println(b)
17
In the code above, a is of type Int. For object b specified by Number, the data type is determined by the assigned value.
val a: Int = 17
var b: Number=a
println(b)
17
b is Int
res12: kotlin.Boolean = true
b=3.14
b
res13: kotlin.Number = 3.14
b is Double
res14: kotlin.Boolean = true
var b: Number=a
println(b)
17
b is Int
res12: kotlin.Boolean = true
b=3.14
b
res13: kotlin.Number = 3.14
b is Double
res14: kotlin.Boolean = true
In the code above, b is a data type depending on the value passed to the object.
Any
"Any" is a data type that has the same process as smart cast.
"Any" is the superclass at the top of every class written by kotlin.
In other words, any class can inherit "Any".
In other words, any class can inherit "Any".
As a result, variable declared as "Any" means that various kinds of values can be assigned.
var a: Any=3
a is Int
res15: kotlin.Boolean = true
a=3.14
a is Double
res16: kotlin.Boolean = true
a="kotlin"
a
res17: kotlin.Any = kotlin
a is String
res18: kotlin.Boolean = true
a is Int
res15: kotlin.Boolean = true
a=3.14
a is Double
res16: kotlin.Boolean = true
a="kotlin"
a
res17: kotlin.Any = kotlin
a is String
res18: kotlin.Boolean = true
๋๊ธ
๋๊ธ ์ฐ๊ธฐ