# 联合体

跟C的联合体类型用法完全一致,联合体的定义方式跟结构体是一样的,

只是把关键字struct改为union,不过在V语言中比较少用到.

## 联合体定义

```v
union MyAnything {
mut:
	bytes [10]u8
	xu64  u64
	xu32  u32
	xu16  u16
	xint  int
}

fn main() {
	mut x := MyAnything{}
	x.xint = 1234
	for i, b in x.bytes {
		println('x.bytes[$i]: $b.hex()')
	}
}
```

## 联合体方法

可以像结构体那样,给联合体添加方法

```v
union MyAnything {
mut:
	bytes [10]u8
	xu64  u64
	xu32  u32
	xu16  u16
	xint  int
}

pub fn (m MyAnything) str() string {
	return 'from union'
}

fn main() {
	mut x := MyAnything{}
	x.xint = 1234
	println(x.str())
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lydiandylin.gitbook.io/vlang/mu-lu/union.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
