1. Banking

기본적인 Go struct 연습.

nomadCoder 강의 실습 #1

  1. main.go

package main

import (
	"fmt"
	accounts "go_tutorial/nomadCoder/practiceStruct/banking"
)

type person struct {
	name    string
	age     int
	favFood []string
}

func main() {
	account := accounts.NewAccount("taeha")
	account.Deposit(10)
	fmt.Println(account.Balance())
	err := account.Withdraw(20)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(account.Balance(), account.Owner())
	fmt.Println(account)

}

2. banking.go

  • struct

  • private, public

    • 첫 글자가 소문자이면 private, 대문자이면 public.

    • function에서도 마찬가지이다. export 가능 여부가 달라짐.

  • method

    • func 옆에 receiver 를 설정함으로써 method 작성이 가능.

    • pointer 사용에 주의하자. (deepCopy, shallowCopy)

  • exception handling

    • try-catch 등의 기능이 없다.

    • error에 대해 개발자가 직접 신경써서 처리해줘야 함.

    • error 가 아닌 경우 nil을 return 한다.

  • String()

    • String() method를 정의함으로써 해당 객체를 print 했을 때 내용을 설정할 수 있다.

Last updated

Was this helpful?