Skip to content
The Chat Archive
claude-3-5-sonnet-20241022· Anthropic LMArena Human Preference 140K

wails.io를 사용하여 기본 프레임 워크를 작성할것이다. 파일 구조를 서술하고 사용 방법을 작성하시오

  1. User

    wails.io를 사용하여 기본 프레임 워크를 작성할것이다. 파일 구조를 서술하고 사용 방법을 작성하시오

  2. Assistant

    Wails.io를 사용한 기본 프레임워크의 구조와 사용방법을 설명하겠습니다.

    1. 프로젝트 구조

    my-wails-app/
    ├── build/            # 빌드 출력 디렉토리
    ├── frontend/         # 프론트엔드 코드
    │   ├── dist/        # 빌드된 프론트엔드 파일
    │   ├── src/         # 소스 코드
    │   │   ├── assets/  # 이미지, 폰트 등
    │   │   ├── components/  # Vue 컴포넌트
    │   │   ├── store/   # 상태 관리
    │   │   ├── App.vue  # 메인 Vue 파일
    │   │   └── main.js  # 진입점
    │   ├── index.html   # HTML 템플릿
    │   └── package.json # 프론트엔드 의존성
    ├── app.go           # 메인 애플리케이션 로직
    ├── main.go          # 진입점
    ├── wails.json       # Wails 설정
    └── go.mod           # Go 모듈 정의
    

    2. 설치 및 설정

    필수 요구사항

    # Go 설치 (1.16+)
    # Node.js 설치 (14+)
    # Wails CLI 설치
    go install github.com/wailsapp/wails/v2/cmd/wails@latest
    

    프로젝트 생성

    # 새 프로젝트 생성
    wails init -n my-wails-app -t vue
    
    # 프로젝트 디렉토리로 이동
    cd my-wails-app
    

    3. 기본 코드 예제

    main.go

    package main
    
    import (
        "context"
        "fmt"
        "github.com/wailsapp/wails/v2"
        "github.com/wailsapp/wails/v2/pkg/options"
    )
    
    func main() {
        app := NewApp()
    
        err := wails.Run(&options.App{
            Title:     "My Wails App",
            Width:     1024,
            Height:    768,
            AssetServer: &assetserver.Options{
                Assets: assets,
            },
            BackgroundColour: &options.RGBA{R: 255, G: 255, B: 255, A: 1},
            OnStartup:        app.startup,
            Bind: []interface{}{
                app,
            },
        })
    
        if err != nil {
            fmt.Println("Error:", err)
        }
    }
    

    app.go

    package main
    
    import "context"
    
    type App struct {
        ctx context.Context
    }
    
    func NewApp() *App {
        return &App{}
    }
    
    func (a *App) startup(ctx context.Context) {
        a.ctx = ctx
    }
    
    // 프론트엔드에서 호출할 수 있는 메서드
    func (a *App) Greet(name string) string {
        return fmt.Sprintf("Hello %s!", name)
    }
    

    frontend/src/App.vue

    <template>
      <div id="app">
        <input v-model="name" placeholder="Enter your name">
        <button @click="greet">Greet</button>
        <p>{{ message }}</p>
      </div>
    </template>
    
    <script>
    import { Greet } from '../wailsjs/go/main/App'
    
    export default {
      data() {
        return {
          name: '',
          message: ''
        }
      },
      methods: {
        async greet() {
          this.message = await Greet(this.name)
        }
      }
    }
    </script>
    

    4. 개발 및 빌드

    개발 모드 실행

    wails dev
    

    프로덕션 빌드

    wails build
    

    5. 주요 기능

    백엔드-프론트엔드 통신

    1. Go에서 JavaScript 호출
    // app.go
    func (a *App) CallJS() {
        runtime.EventsEmit(a.ctx, "from-backend", "Hello from Go!")
    }
    
    // frontend
    window.runtime.EventsOn("from-backend", (data) => {
        console.log(data)
    })
    
    1. JavaScript에서 Go 호출
    // app.go
    func (a *App) HandleFrontendCall(data string) string {
        return "Received: " + data
    }
    
    // frontend
    import { HandleFrontendCall } from '../wailsjs/go/main/App'
    
    async function callBackend() {
        const response = await HandleFrontendCall("Hello from JS!")
        console.log(response)
    }
    

    6. 유용한 기능들

    시스템 트레이

    import "github.com/wailsapp/wails/v2/pkg/menu"
    
    func createSystemTray() *menu.TrayMenu {
        tray := menu.NewTrayMenu()
        tray.AddText("My App")
        tray.AddSeparator()
        tray.AddItem("Quit", nil, func(_ *menu.CallbackData) {
            runtime.Quit(app.ctx)
        })
        return tray
    }
    

    파일 다이얼로그

    func (a *App) OpenFileDialog() string {
        selection, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{
            Title: "Select File",
            Filters: []runtime.FileFilter{
                {
                    DisplayName: "Images (*.png;*.jpg)",
                    Pattern:     "*.png;*.jpg",
                },
            },
        })
        if err != nil {
            return ""
        }
        return selection
    }
    

    7. 디버깅

    백엔드 디버깅

    import "log"
    
    func (a *App) Debug(message string) {
        log.Printf("Debug: %s", message)
    }
    

    프론트엔드 디버깅

    • 개발 모드에서 Chrome DevTools 사용 가능
    • wails dev 실행 시 자동으로 DevTools 활성화

    8. 배포

    Windows

    wails build -platform windows/amd64
    

    macOS

    wails build -platform darwin/universal
    

    Linux

    wails build -platform linux/amd64
    

    이 기본 프레임워크를 바탕으로 필요한 기능을 추가하여 개발하시면 됩니다. 특정 기능이나 자세한 설명이 필요하시다면 말씀해 주세요.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.