쉽고 빠른 Go 시작하기 - 3 (~1.11 structs)

Melon Coder·2024년 5월 27일

Go

목록 보기
3/14

if

go언어에서는 다음과 같이 조건식과 함께 변수를 선언할 수 있다.
그리고 else 도 생략이 가능하다.

func canIDrink(age int) bool {
	if koreanAge := age + 2; koreanAge < 19 {
    	return false;
    }
    return true;
}

switch

func canIDrink(age int) bool {
	switch koreanAge:= age+2; koreanAge {
    	case 10:
        	return false;
        case 18:
        	return true;
    }
    return false;
}

pointer

func main() {
	a:= 2; 
    b:= &a;	// b에는 a의 메모리 주소값이 저장된다.
    *b= 30; // 메모리 주소에 *를 앞에 붙여 주소에 담긴 값을 변경할 수 있다. 
    		// 최종적으로 a의 값이 변경된다.
}

array & slice

[] 안에 배열의 길이를 지정할 수 있고 안해도 상관없음.
append는 슬라이스한 배열을 리턴시켜준다.

func main() {
	students:= []string{"lee", "ahn", "yoo"};
    students = append(students, "kim")
    fmt.Println(students)
   	
    >>> [lee ahn yoo kim]
}

map

func main() {
	melon:= map[string] string{"name": "melon", "age": "28"}
}

struct

func main() {
	type person struct {
		name string
		age int
		isArmy bool
		favoriteFood []string
	}

	favoriteFood:= []string {"pasta", "hamburger"}
	melon:= person{name: "melon", age: 28, isArmy: true, favoriteFood: favoriteFood}

	fmt.Println(melon)
    
    >>> {melon 28 true [pasta hamburger]}
}
profile
About me: https://resume-seven-beige.vercel.app/

0개의 댓글