← 가이드 목록으로 ← Back to guides

Go 언어 핵심 문법 완벽 가이드 Complete Guide to Go Language Core Syntax

Go는 Google에서 개발한 단순하고 효율적인 프로그래밍 언어입니다. 강력한 동시성 지원과 빠른 컴파일 속도로 클라우드 인프라와 백엔드 개발에 널리 사용됩니다.

Go is a simple and efficient programming language developed by Google. It's widely used for cloud infrastructure and backend development due to its powerful concurrency support and fast compilation.

기본 구조 Basic Structure

package main

import "fmt"

func main() {
  fmt.Println("Hello, Go!")
}
package main

import "fmt"

func main() {
  fmt.Println("Hello, Go!")
}

변수와 타입 Variables and Types

// 명시적 선언
var name string = "Go"
var age int = 15

// 짧은 선언 (:=)
count := 10
isActive := true

// 상수
const Pi = 3.14159
// 명시적 선언
var name string = "Go"
var age int = 15

// Short declaration (:=)
count := 10
isActive := true

// Constants
const Pi = 3.14159

Goroutine과 Channel Goroutine and Channel

Go의 가장 강력한 기능인 동시성 프로그래밍입니다. goroutine은 경량 스레드이고, channel은 goroutine 간 통신 수단입니다.

Go's most powerful feature for concurrent programming. goroutine is a lightweight thread, and channel is for communication between goroutines.

// Goroutine 시작
go doWork()

// Channel 생성
ch := make(chan int)

// Channel로 데이터 전송
go func() {
  ch <- 42
}()

// Channel에서 데이터 수신
result := <-ch
// Goroutine 시작
go doWork()

// Channel 생성
ch := make(chan int)

// Channel로 데이터 전송
go func() {
  ch <- 42
}()

// Channel에서 데이터 수신
result := <-ch

에러 처리 Error Handling

Go는 예외 대신 명시적인 에러 반환을 사용합니다.

Go uses explicit error returns instead of exceptions.

func divide(a, b float64) (float64, error) {
  if b == 0 {
    return 0, errors.New("division by zero")
  }
  return a / b, nil
}

result, err := divide(10, 2)
if err != nil {
  fmt.Println("Error:", err)
  return
}
func divide(a, b float64) (float64, error) {
  if b == 0 {
    return 0, errors.New("division by zero")
  }
  return a / b, nil
}

result, err := divide(10, 2)
if err != nil {
  fmt.Println("Error:", err)
  return
}

구조체와 인터페이스 Struct and Interface

// 구조체
type User struct {
  ID int
  Name string
}

// 메서드
func (u User) Greet() string {
  return "Hello, " + u.Name
}

// 인터페이스
type Greeter interface {
  Greet() string
}
// Struct
type User struct {
  ID int
  Name string
}

// Method
func (u User) Greet() string {
  return "Hello, " + u.Name
}

// Interface
type Greeter interface {
  Greet() string
}

💡 Go 타자 연습 팁 💡 Go Typing Practice Tips

Go의 독특한 문법인 :=, <-, func, go 키워드에 익숙해지세요. 특히 goroutine과 channel 패턴을 반복 연습하면 동시성 코드를 더 빠르게 작성할 수 있습니다.

Get familiar with Go's unique syntax like :=, <-, func, and go keywords. Practicing goroutine and channel patterns helps write concurrent code faster.