一川·MOLIPRE·BLOG

代码高亮测试

Python 代码示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def fibonacci(n):
    """生成斐波那契数列"""
    a, b = 0, 1
    for _ in range(n):
        yield a
        a, b = b, a + b

# 使用示例
fib_list = list(fibonacci(10))
print(fib_list)  # [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

JavaScript 代码示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// 异步函数示例
async function fetchData(url) {
    try {
        const response = await fetch(url);
        const data = await response.json();
        return data;
    } catch (error) {
        console.error('获取数据失败:', error);
        throw error;
    }
}

// 使用箭头函数
const double = x => x * 2;
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(double);

HTML/CSS 示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>示例页面</title>
    <style>
        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .code-block {
            background: #f8f9fa;
            border: 1px solid #e9ecef;
            border-radius: 4px;
            padding: 1rem;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Hello World!</h1>
    </div>
</body>
</html>

Go 语言示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, 欢迎访问 %s!", r.URL.Path[1:])
}

func main() {
    http.HandleFunc("/", handler)
    fmt.Println("服务器启动在 http://localhost:8080")
    http.ListenAndServe(":8080", nil)
}

Bash 脚本示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash

# 备份脚本
BACKUP_DIR="/backup"
LOG_FILE="/var/log/backup.log"

backup_files() {
    local source_dir=$1
    local backup_name=$2
    
    echo "$(date): 开始备份 $source_dir" >> "$LOG_FILE"
    tar -czf "$BACKUP_DIR/$backup_name-$(date +%Y%m%d).tar.gz" "$source_dir"
    
    if [ $? -eq 0 ]; then
        echo "$(date): 备份成功" >> "$LOG_FILE"
    else
        echo "$(date): 备份失败" >> "$LOG_FILE"
        return 1
    fi
}

#测试 #代码高亮