代码片段仓库

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

代码片段

医疗影像处理 Go

DICOM解析

package main

import (
    "github.com/sinaly/dicom"
    "os"
)

func ReadDicom(path string) (*[][]float64, error) {
    file, err := os.Open(path)
    if err != nil {
        return nil, err
    }
    defer file.Close()

    dcm, err := dicom.Parse(file)
    if err != nil {
        return nil, err
    }

    img, ok := dcm.Image()
    if !ok {
        return nil, errors.New("invalid IMAGE")
    }

    pixels := make([][]float64, img.Bounds().Dy())
    for y := 0; y < img.Bounds().Dy(); y++ {
        row := make([]float64, img.Bounds().Dx())
        for x := 0; x < img.Bounds().Dx; x++ {
            row[x] = img.Get(x, y)
        }
        pixels[y] = row
    }

    return pixels, nil
}
元宇宙渲染 Go

3D场景管理

package main

import (
    "github.com/golang/vulkan"
)

type Renderer struct {
    device     vulkan.Device
    swapchain  vulkan.Swapchain
    pipeline   vulkan.Pipeline
    commandBuf vulkan.CommandBuffer
}

func (m *Renderer) RenderScene(scene interface{}) {
    // Begin render pass
    metal.BeginRenderPass(m.commandBuf, vulkan.STB_INIT)

    // Render meshes
    for _, obj := range scene.Objects() {
        metal.BindVertexBuffer(m.commandBuf, obj.VB)
        metal.BindTexture(m.commandBuf, obj.Texure, 0)
        metal.Draw(m.commandBuf, 0, obj.VertCount)
    }

    // End pass and present
    metal.EndRenderPass(m.commandBuf)
    metal.Present(m.swapchain, m.commandBuf)
}
元宇宙渲染 Go

3D场景管理

package main

import (
    "github.com/golang/vulkan"
)

type Renderer struct {
    device     vulkan.Device
    swapchain  vulkan.Swapchain
    pipeline   vulkan.Pipeline
    commandBuf vulkan.CommandBuffer
}

func (m *Renderer) RenderScene(scene interface{}) {
    // Begin render pass
    metal.BeginRenderPass(m.commandBuf, vulkan.STB_INIT)

    // Render meshes
    for _, obj := range scene.Objects() {
        metal.BindVertexBuffer(m.commandBuf, obj.VB)
        metal.BindTexture(m.commandBuf, obj.Texure, 0)
        metal.Draw(m.commandBuf, 0, obj.VertCount)
    }

    // End pass and present
    metal.EndRenderPass(m.commandBuf)
    metal.Present(m.swapchain, m.commandBuf)
}
AI伦理审查 Go

偏见检测算法

package main

import (
    "math"
    "sort"
)

type BiasResult struct {
    Group     string
    PositiveRate float64
}

func DetectBias(trainData [][]float64, labels []int, groupCol int) []*BiasResult {
    results := make(map[string][]int)

    // Collect statistics per group
    for i, row := range trainData {
        group := fmt.Sprintf("%v", row[groupCol])
        results[group] = append(results[group], labels[i])
    }

    // Calculate positive rate
    output := []*BiasResult{}
    for group, data := range results {
        sum := 0
        for _, v := range data {
            sum += v
        }
        rate := float64(sum) / float64(len(data))
        output = append(output, &BiasResult{Group: group, PositiveRate: rate})
    }

    return output
}
元宇宙渲染 Go

3D场景管理

package main

import (
    "github.com/golang/vulkan"
)

type Renderer struct {
    device     vulkan.Device
    swapchain  vulkan.Swapchain
    pipeline   vulkan.Pipeline
    commandBuf vulkan.CommandBuffer
}

func (m *Renderer) RenderScene(scene interface{}) {
    // Begin render pass
    metal.BeginRenderPass(m.commandBuf, vulkan.STB_INIT)

    // Render meshes
    for _, obj := range scene.Objects() {
        metal.BindVertexBuffer(m.commandBuf, obj.VB)
        metal.BindTexture(m.commandBuf, obj.Texure, 0)
        metal.Draw(m.commandBuf, 0, obj.VertCount)
    }

    // End pass and present
    metal.EndRenderPass(m.commandBuf)
    metal.Present(m.swapchain, m.commandBuf)
}
元宇宙渲染 Go

3D场景管理

package main

import (
    "github.com/golang/vulkan"
)

type Renderer struct {
    device     vulkan.Device
    swapchain  vulkan.Swapchain
    pipeline   vulkan.Pipeline
    commandBuf vulkan.CommandBuffer
}

func (m *Renderer) RenderScene(scene interface{}) {
    // Begin render pass
    metal.BeginRenderPass(m.commandBuf, vulkan.STB_INIT)

    // Render meshes
    for _, obj := range scene.Objects() {
        metal.BindVertexBuffer(m.commandBuf, obj.VB)
        metal.BindTexture(m.commandBuf, obj.Texure, 0)
        metal.Draw(m.commandBuf, 0, obj.VertCount)
    }

    // End pass and present
    metal.EndRenderPass(m.commandBuf)
    metal.Present(m.swapchain, m.commandBuf)
}

为什么选择CodeSnippets?

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

智能搜索

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

语法高亮

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

多设备同步

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

热门分类

浏览最受欢迎的代码分类