.NET CLI를 사용하여 F# 시작

이 문서에서는 .NET CLI를 사용하여 모든 운영 체제(Windows, macOS 또는 Linux)에서 F#을 시작하는 방법을 설명합니다. 콘솔 애플리케이션에서 호출하는 클래스 라이브러리를 사용하여 다중 프로젝트 솔루션을 빌드하는 방법을 안내합니다.

필수 조건

시작하려면 최신 .NET SDK를 설치해야 합니다.

이 문서에서는 명령줄을 사용하는 방법을 알고 있으며 기본 텍스트 편집기가 있다고 가정합니다. 아직 사용하지 않는 경우 Visual Studio Code 는 F#의 텍스트 편집기로서 유용한 옵션입니다.

간단한 다중 프로젝트 솔루션 빌드

명령 프롬프트/터미널을 열고 dotnet new 명령을 사용하여 다음과 같은 FSharpSample새 솔루션 파일을 만듭니다.

dotnet new sln -o FSharpSample

다음 디렉터리 구조는 이전 명령을 실행한 후에 생성됩니다.

FSharpSample
    ├── FSharpSample.sln

클래스 라이브러리 작성

디렉터리를 FSharpSample변경합니다.

명령을 dotnet new 사용하여 Library라는 src 폴더에 클래스 라이브러리 프로젝트를 만듭니다.

dotnet new classlib -lang "F#" -o src/Library

다음 디렉터리 구조는 이전 명령을 실행한 후에 생성됩니다.

└── FSharpSample
    ├── FSharpSample.sln
    └── src
        └── Library
            ├── Library.fs
            └── Library.fsproj

Library.fs의 내용을 다음 코드로 바꿉니다.

module Library

open System.Text.Json

let getJson value =
    let json = JsonSerializer.Serialize(value)
    value, json

Library dotnet sln add 명령을 사용하여 솔루션에 프로젝트를 FSharpSample 추가합니다.

dotnet sln add src/Library/Library.fsproj

실행 dotnet build 하여 프로젝트를 빌드합니다. 빌드할 때 해결되지 않은 종속성이 복원됩니다.

클래스 라이브러리를 사용하는 콘솔 애플리케이션 작성

명령을 dotnet new 사용하여 App이라는 src 폴더에 콘솔 애플리케이션을 만듭니다.

dotnet new console -lang "F#" -o src/App

다음 디렉터리 구조는 이전 명령을 실행한 후에 생성됩니다.

└── FSharpSample
    ├── FSharpSample.sln
    └── src
        ├── App
        │   ├── App.fsproj
        │   ├── Program.fs
        └── Library
            ├── Library.fs
            └── Library.fsproj

Program.fs 파일의 내용을 다음 코드로 바꿉니다.

open System
open Library

[<EntryPoint>]
let main args =
    printfn "Nice command-line arguments! Here's what System.Text.Json has to say about them:"

    let value, json = getJson {| args=args; year=System.DateTime.Now.Year |}
    printfn $"Input: %0A{value}"
    printfn $"Output: %s{json}"

    0 // return an integer exit code

dotnet add reference를 Library 사용하여 프로젝트에 대한 참조를 추가합니다.

dotnet add src/App/App.fsproj reference src/Library/Library.fsproj

App 다음 명령을 사용하여 솔루션에 FSharpSample 프로젝트를 추가합니다.dotnet sln add

dotnet sln add src/App/App.fsproj

NuGet 종속성을 dotnet restore 복원하고 실행 dotnet build 하여 프로젝트를 빌드합니다.

디렉터리를 콘솔 프로젝트로 src/App 변경하고 인수로 전달 Hello World 되는 프로젝트를 실행합니다.

cd src/App
dotnet run Hello World

다음 결과가 보일 것입니다.

Nice command-line arguments! Here's what System.Text.Json has to say about them:
Input: { args = [|"Hello"; "World"|] year = 2021 }
Output: {"args":["Hello","World"],"year":2021}

다음 단계

다음으로, F# 둘러보기를 검사 다양한 F# 기능에 대해 자세히 알아보세요.