> For the complete documentation index, see [llms.txt](https://rafli-ramadhan.gitbook.io/golang-example-code-by-topic/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rafli-ramadhan.gitbook.io/golang-example-code-by-topic/konversi-tipe-data.md).

# Konversi Tipe Data

Konversi tipe data digunakan untuk mengubah value dengan tipe data tertentu ke tipe data yang lain.

## Contoh code konversi integer ke float

Value dengan tipe data integer bisa diubah menjadi float, karena di tipe data float terdapat integer.

```go
package main

import (
	"fmt"
)

func main() {
	var num1 int = 2
	var num2 float64 = float64(num1)
	fmt.Println(num2)
	fmt.Printf("%T\n", num2)
}
```

```
2
float64
```

## Contoh code konversi float ke integer

Value dengan tipe data float dapat dikonversi menjadi integer dan akan dibulatkan ke bawah.

```go
package main

import (
	"fmt"
)

func main() {
	var num1 float64 = 2.9
	var num2 int = int(num1)
	fmt.Println(num2)
	fmt.Printf("%T", num2)
}
```

```
2
int
```

## Contoh code konversi boolean ke string

Boolean tidak dapat dikonversi menjadi string

```go
package main

import (
	"fmt"
)

func main() {
	var isFalse bool = true
	fmt.Printf(string(isFalse))
}
```

```
cannot convert isFalse (variable of type bool) to type string
```

## Contoh code konversi string menjadi rune dan byte

Konversi string menjadi byte akan menghasilkan angka yang mewakili karakter ASCII, sementara konversi menjadi rune akan menghasilkan angka yang mewakili karakter Unicode.

```go
package main
 
import (
    "fmt"
)
 
func main() {
    s := "GÖLANG PROGRAMMING golang programming" // Ö is a non-ASCII character 
 
    s_rune := []rune(s)
    s_byte := []byte(s)
     
    fmt.Println(s_rune)
    fmt.Println(s_byte)
}
```

```
[71 214 76 65 78 71 32 80 82 79 71 82 65 77 77 73 78 71 32 103 111 108 97 110 103 32 112 114 111 103 114 97 109 109 105 110 103]
[71 195 150 76 65 78 71 32 80 82 79 71 82 65 77 77 73 78 71 32 103 111 108 97 110 103 32 112 114 111 103 114 97 109 109 105 110 103]
```

Reference :

{% embed url="<https://golangdocs.com/rune-in-golang>" %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://rafli-ramadhan.gitbook.io/golang-example-code-by-topic/konversi-tipe-data.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.
