solidity 03. 데이터 타입

카테고리 없음 2022. 1. 15. 00:55
반응형

어떤 언어로든 프로그램을 작성하다 보면 다양한 정보를 저장하기 위해 다양한 변수를 사용해야 한다 

변수는 값을 저장하기 위해 예약된 메모리 위치다 

변수를 생성할 때 메모리에 약간의 공간을 예약한다는 것을 의미한다 

다양한 데이터 유형의 정보를 저장할수 있다

변수의 데이터 유형에 따라 운영 체제는 메모리를 할당하고 메모리에 저장할 수 있는 항목을 결정한다 

 

Solidity는 프로그래머에게 내장 데이터 유형과 사용자 정의 데이터 유형을 다양하게 제공합니다. 

 

타입 명  설명 예제 
string UTF-8 인코딩 문자열 "Hello World!"
bool true, false 상수값  true 또는 false
int 정수형 값  0 또는 -300 또는 3123 등 
uint 부호 없는 정수  0 또는 100 또는 999 등 
fixed / ufixed  부동 소수점  30.01 또는 -23.11 또는 3.14 등 
address 이더리움 주소값  0x14rae189c8dasd293c8e 

 

배열 

uint[] accounts;

 

구조체 

struct User {
    uint id;
    string name;
    uint[] friendIds;
}

 

열거형식 

enum Color {
    RED, BLUE, GREEN
}

 

 

 

mapping 타입 

_KeyType은 모든 기본 제공 값 유형, 바이트, 문자열 또는 모든 계약 또는 열거형 유형일 수 있다

매핑, 구조체 또는 배열 유형과 같은 다른 사용자 정의 또는 복합 유형은 허용되지 않는다

_ValueType은 매핑, 배열 및 구조체를 포함한 모든 유형이 될 수 있다 

mapping(_KeyType => _ValueType) _VariableName


//ex)
mapping(address => uint) public balances;

function update(uint newBalance) public {
    balances[msg.sender] = newBalance;
}

 

 

 

 

 

더 자세한 사항은 공식 문서를 참고 하자 

https://docs.soliditylang.org/en/v0.8.11/types.html#

 

Types — Solidity 0.8.11 documentation

Types Solidity is a statically typed language, which means that the type of each variable (state and local) needs to be specified. Solidity provides several elementary types which can be combined to form complex types. In addition, types can interact with

docs.soliditylang.org

반응형
: