December 28, 2023
Tutorial: Create a Go module - The Go Programming Language
코드는 package에 그루핑 되고, 패키지들은 모듈에 그루핑 됨
아래의 명령어와 코드를 통해 새로운 모듈 생성
go mod init example.com/greetings
package greetings
import "fmt"
// Hello returns a greeting for the named person.
func Hello(name string) string {
// Return a greeting that embeds the name in a message.
message := fmt.Sprintf("Hi, %v. Welcome!", name)
return message
}
Go에서는 같은 패키지가 아닌 외부에서 호출 가능한 함수는 대문자로 시작함
:= 연산자는 변수 선언과 동시에 초기화가 가능하며, 위의 선언은 아래의 코드와 동일
var message string
message = fmt.Sprintf("Hi, %v. Welcome!", name)