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

  • Home
  • SEARCH
  • 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 8439883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:07:50+00:00 2026-06-10T08:07:50+00:00

Is it possible to convert a pointer to certain value to a slice? For

  • 0

Is it possible to convert a pointer to certain value to a slice?

For example, I want to read single byte from io.Reader into uint8 variable. io.Reader.Read accepts a slice as its argument, so I cannot simply provide it a pointer to my variable as I’d do in C.

I think that creating a slice of length 1, capacity 1 from a pointer is safe operation. Obviously, it should be the same as creating a slice from an array of length 1, which is allowed operation. Is there an easy way to do this with plain variable? Or maybe I do not understand something and there are reasons why this is prohibited?

  • 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-10T08:07:51+00:00Added an answer on June 10, 2026 at 8:07 am

    A slice is not only a pointer, like an array in C. It also contains the length and capacity of the data, like this:

    struct {
      ptr *uint8
      len int
      cap int
    }
    

    So, yes, you will need to create a slice. Simplest way to create a slice of the var a uint8 would be []uint8{a}

    a := uint8(42)
    fmt.Printf("%#v\n", []uint8{a})
    

    (But after rereading your question, this is not a solution as all)

    But if you wish to create the slice from the variable, pointing to the same space of memory, you could use the unsafe package. This is most likely to be discouraged.

    fmt.Printf("%#v\n", (*[1]uint8)(unsafe.Pointer(&a))[:] )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to convert HTML + CSS into HTML for a system that
Is it possible to convert facebook data (ex. user_pic) into a DisplayObject so I
Is it possible, in C#, to convert a multi-dimensional array into a 1D array
Is it possible to convert Data report to a PDF from code? As of
I want to store the static value in string pointer is it posible? If
Possible Duplicate: How do I convert a string into an integer in objective C?
Possible Duplicate: why isnt it legal to convert (pointer to pointer to non-const) to
Possible Duplicate: Convert a number to the shortest possible character string while retaining uniqueness
Is it possible to convert the font of a matlab plot to be the
Is it possible to convert a FF Jetpack extension to a chrome's one? I've

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.