创建包 #

一、包结构 #

1.1 生成包 #

julia
using Pkg
Pkg.generate("MyPackage")

1.2 目录结构 #

text
MyPackage/
├── Project.toml
├── src/
│   └── MyPackage.jl
└── test/
    └── runtests.jl

1.3 Project.toml #

toml
name = "MyPackage"
uuid = "12345678-1234-1234-1234-123456789012"
authors = ["Your Name <you@example.com>"]
version = "0.1.0"

[deps]

[compat]
julia = "1.8"

1.4 主模块文件 #

julia
module MyPackage

export greet, add

greet(name) = println("Hello, $name!")
add(a, b) = a + b

end

二、开发包 #

2.1 激活包 #

julia
using Pkg
Pkg.activate("MyPackage")
Pkg.instantiate()

2.2 添加依赖 #

julia
Pkg.add("DataFrames")

2.3 开发模式 #

julia
Pkg.develop(path="MyPackage")

2.4 使用包 #

julia
using MyPackage

greet("Julia")
add(1, 2)

三、测试 #

3.1 测试目录 #

text
test/
├── runtests.jl
└── test_basic.jl

3.2 runtests.jl #

julia
using Test
using MyPackage

@testset "MyPackage tests" begin
    @testset "greet" begin
        @test greet("World") === nothing
    end
    
    @testset "add" begin
        @test add(1, 2) == 3
        @test add(1.0, 2.0) == 3.0
        @test add(-1, 1) == 0
    end
end

3.3 运行测试 #

julia
Pkg.test("MyPackage")

3.4 添加Test依赖 #

在Project.toml中:

toml
[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]

四、文档 #

4.1 文档字符串 #

julia
"""
    greet(name)

Print a greeting to `name`.

# Examples
```julia
julia> greet("Julia")
Hello, Julia!

“”" function greet(name) println(“Hello, $name!”) end

text

### 4.2 Documenter.jl

添加文档生成:

```julia
Pkg.add("Documenter")

创建docs/make.jl

julia
using Documenter, MyPackage

makedocs(
    sitename = "MyPackage",
    modules = [MyPackage],
    pages = [
        "Home" => "index.md",
        "API" => "api.md"
    ]
)

五、完整示例 #

5.1 Project.toml #

toml
name = "MathUtils"
uuid = "12345678-1234-1234-1234-123456789012"
authors = ["Your Name <you@example.com>"]
version = "0.1.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]

[compat]
julia = "1.8"

5.2 src/MathUtils.jl #

julia
module MathUtils

export fibonacci, is_prime, factorial_safe

"""
    fibonacci(n)

Calculate the nth Fibonacci number.
"""
function fibonacci(n::Int)
    n < 0 && error("n must be non-negative")
    n <= 1 && return n
    a, b = 0, 1
    for _ in 2:n
        a, b = b, a + b
    end
    return b
end

"""
    is_prime(n)

Check if `n` is a prime number.
"""
function is_prime(n::Int)
    n < 2 && return false
    for i in 2:isqrt(n)
        n % i == 0 && return false
    end
    return true
end

"""
    factorial_safe(n)

Calculate factorial with overflow checking.
"""
function factorial_safe(n::Int)
    n < 0 && error("n must be non-negative")
    n <= 1 && return 1
    result = 1
    for i in 2:n
        result, overflow = Base.mul_with_overflow(result, i)
        overflow && error("Factorial overflow")
    end
    return result
end

end

5.3 test/runtests.jl #

julia
using Test
using MathUtils

@testset "MathUtils tests" begin
    @testset "fibonacci" begin
        @test fibonacci(0) == 0
        @test fibonacci(1) == 1
        @test fibonacci(10) == 55
        @test_throws ErrorException fibonacci(-1)
    end
    
    @testset "is_prime" begin
        @test is_prime(2) == true
        @test is_prime(17) == true
        @test is_prime(4) == false
        @test is_prime(1) == false
    end
    
    @testset "factorial_safe" begin
        @test factorial_safe(0) == 1
        @test factorial_safe(5) == 120
        @test_throws ErrorException factorial_safe(-1)
    end
end

六、注册包 #

6.1 准备工作 #

  1. 代码托管在GitHub
  2. 添加CI配置(GitHub Actions)
  3. 确保测试通过
  4. 添加文档

6.2 注册到General #

  1. Fork https://github.com/JuliaRegistries/General
  2. 添加注册条目
  3. 提交PR

或使用Registrator:

在GitHub issue或PR中评论:

text
@JuliaRegistrator register

七、总结 #

本章我们学习了:

  1. 包结构:目录和配置文件
  2. 开发包:激活、添加依赖、开发模式
  3. 测试:Test框架和测试文件
  4. 文档:文档字符串和Documenter
  5. 注册包:发布到General注册表

接下来让我们学习Julia的输入输出!

最后更新:2026-03-27