GO 언어 강의를 듣다가 강사님이 '자, 이럴 경우에 메소드가 필요해요!'라고 한 얘기를 듣고 의아할 수 밖에 없었다.
'지금까지 'func' 쓰면서 다 작성했는데 이제 메소드를 작성한다고???'
나의 주력언어는 자바인지라 지금까지 '함수=메소드'인줄로만 알고 있었는데, 찾아보니 함수가 메소드보다 포괄적인 의미라는 것을 알고 '아직 갈 길이 멀었구나..' 싶었다.
함수(Function)와 메소드(Method)는 짜여진 코드를 수행하고 파라미터를 가질 수 있으며 이름으로 호출되는 등의 방식은 비슷하나, 차이점은 함수는 객체에 독립적이며 메소드는 객체와 연관된 기능을 수행한다는 것이다.
예를 들자면, 함수는 print()나 sum()과 같이 독립적으로 수행할 수 있으나 메소드는 obj.method()와 같이 객체 내의 기능을 호출하는 것이다.
참고로 JAVA와 C#은 메소드만 사용하고, C언어는 함수만 사용한다.
C++과 파이썬은 클래스의 사용여부에 따라 달라진다고 한다.
Go에서 함수는 동일한 input에는 항상 동일한 output이 리턴되며, 이 때 type은 해당 함수의 인자로 사용되고, 메소드는 특정 receiver(type)과 연관된 기능이며, 메소드는 해당 receiver가 가지고 있는 properties에 접근할 수 있다.
(receiver 이름은 struct의 앞글자를 소문자로 바꿔서 작성하는 것이 암묵적인 규칙이라고 한다.)
func(reciver_name Type) method_name(parameter_list)(return_type){
// Code
}
// Syntax of method in Golang
다음에 GeeksForGeeks 참고해서 GO 메소드에 대한 내용도 정리해봐야겠다.
참고
https://stackoverflow.com/questions/155609/whats-the-difference-between-a-method-and-a-function
What's the difference between a method and a function?
Can someone provide a simple explanation of methods vs. functions in OOP context?
stackoverflow.com
https://medium.com/@ishagirdhar/methods-vs-functions-in-golang-c60586bfa6b4
Methods vs Functions in Golang
Methods or functions? Functions or methods?
medium.com
https://www.geeksforgeeks.org/methods-in-golang/
Methods in Golang - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
www.geeksforgeeks.org
'개발이야기 > 기타' 카테고리의 다른 글
SOLID, DRY, KISS AND YAGNI (0) | 2021.07.07 |
---|---|
Kubernetes(쿠버네티스)의 개념 (0) | 2021.06.02 |
도커(Docker) (0) | 2021.06.01 |
Web Framework란? (0) | 2021.05.27 |
윈도우 명령어 정리 (0) | 2021.05.14 |