Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8886723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:37:37+00:00 2026-06-14T21:37:37+00:00

I have the following code for implementing a splice (that is, given a byte

  • 0

I have the following code for implementing a splice (that is, given a byte slice full, another byte slice part, and an int pos representing the position in full that I want to overwrite with part):

package main

import (
    "fmt"
    "bytes"
)

func main() {
    full := []byte{0,0,0,0,0,0,0}
    part := []byte{1,1,1}

    newFull1 := splice(full, part, 2)
    fmt.Println(newFull1)
    // [0 0 1 1 1 0 0]

    newFull2 := splice(full, part, 3)
    fmt.Println(newFull2)
    // [0 0 0 1 1 1 0]
}

func splice(full []byte, part []byte, pos int) []byte {
    return bytes.Join([][]byte{full[:pos], part, full[len(full[:pos])+len(part):]}, []byte{})
}

Basically, my method does a join of 3 byte slices: the first part of full that doesn’t get overwritten by part, all of part, and then the remaining part of full. Is there a better/more idiomatic way of doing this? I wasn’t able to find a method that implemented this in the standard library.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:37:38+00:00Added an answer on June 14, 2026 at 9:37 pm

    If you know part is completely within the bounds of full, you can use the copy function.

    func main() {
        full := []byte{0, 0, 0, 0, 0, 0, 0}
        part := []byte{1, 1, 1}
    
        copy(full[2:], part)
        fmt.Println(full)
    }
    

    playground

    That overwrites full though. If you wanted to preserve the original, you could make a copy first with the append function.

    func main() {
        full := []byte{0, 0, 0, 0, 0, 0, 0}
        part := []byte{1, 1, 1}
    
        newFull := append([]byte{}, full...)
        copy(newFull[2:], part)
        fmt.Println("newFull:      ", newFull)
        fmt.Println("original full:", full)
    }
    

    playground

    Note that this still has the limitation of your original code that part must fit within the bounds of full.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that defines a getParts method to find a given
I have following code that I am compiling in a .NET 4.0 project namespace
I am implementing INotifyPropertyChanged and as part of that interface I have the member
I have following code that traverses through a list of people and calls a
I am implementing a Binary Search tree in C++ I have the following code
I am implementing the Jquery UI autocomplete. I have the following code. Application.js $(function()
I have the following HTML for buttons implementing sliding doors technique that look fine
In the following code, I'm implementing an interface, and then deriving from that class
I have following code in initialization im = imread('Image02.tif'); figure(); imagesc(im); colormap(gray); [hImage hfig
I have following code <div id=main> <div id=one> </div> <div id=two> </div> <div id=three>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.