


Go language string to binary file writing
This article describes how to convert the string "REDIS" into binary data and write it to a file using Go language so that it appears as "52 45 44 49 53" when viewed using vim
's %!xxd
command. It can be achieved without the help of binary
package.
First, understand the binary representation of the string "REDIS". Using the hexdump
command, we can see its hexadecimal representation:
echo 'REDIS' | hexdump 0000000 52 45 44 49 53 0a 0000006
Ignore the last newline character 0a
, and the binary representation of "REDIS" is 52 45 44 49 53
. Therefore, writing the string directly to the file can achieve the purpose.
The following Go code implements this function:
package main import ( "fmt" "io" "log" "os" ) func main() { file, err := os.Create("redis.bin") // Create a file using os.Create to avoid appending mode if err != nil { log.Fatal(err) } defer file.Close() n, err := io.WriteString(file, "REDIS") if err != nil { log.Fatal(err) } fmt.Printf("write %d bytes\n", n) }
After running, use hexdump
to view the generated redis.bin
file:
cat redis.bin | hexdump 0000000 52 45 44 49 53 0000005
The results show that the file content is 52 45 44 49 53
, which is consistent with the expectations. Open the redis.bin
file through vim
, and use %!xxd
command to display the hexadecimal representation of 52 45 44 49 53
. This method directly utilizes the string writing function of Go language to complete the conversion and writing of strings to binary files concisely and efficiently. Note that using os.Create
instead of os.OpenFile
ensures that files are created in write mode to avoid potential append problems.
The above is the detailed content of How to convert the string 'REDIS' to binary and write to a file using Go language?. For more information, please follow other related articles on the PHP Chinese website!

Redis是现在最热门的key-value数据库,Redis的最大特点是key-value存储所带来的简单和高性能;相较于MongoDB和Redis,晚一年发布的ES可能知名度要低一些,ES的特点是搜索,ES是围绕搜索设计的。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于redis的一些优势和特点,Redis 是一个开源的使用ANSI C语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式存储数据库,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis Cluster集群收缩主从节点的相关问题,包括了Cluster集群收缩概念、将6390主节点从集群中收缩、验证数据迁移过程是否导致数据异常等,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于原子操作中命令原子性的相关问题,包括了处理并发的方案、编程模型、多IO线程以及单命令的相关内容,下面一起看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了bitmap问题,Redis 为我们提供了位图这一数据结构,位图数据结构其实并不是一个全新的玩意,我们可以简单的认为就是个数组,只是里面的内容只能为0或1而已,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,下面一起来看一下,希望对大家有帮助。

redis error就是redis数据库和其组合使用的部件出现错误,这个出现的错误有很多种,例如Redis被配置为保存数据库快照,但它不能持久化到硬盘,用来修改集合数据的命令不能用。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.