Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Basic Type Casting | Section
Practice
Projects
Quizzes & Challenges
Quizze
Challenges
/
Getting Started with Go

bookBasic Type Casting

Swipe um das Menü anzuzeigen

Typecasting is the process of converting data from one data type to another. However, it's important to note that it's not always possible to convert data from one data type to another. For example, we can convert a float to an int and vice versa. However, it wouldn't make sense to convert a string to an int, and thus it's not possible.

There are two types of type conversions or typecasting. One of them is Implicit Type Casting. Implicit type casting occurs when Go automatically converts one type to another when it's safe and unambiguous. For example, when we assign an integer value to a float variable, it is treated as a float automatically, as no data can be lost during the conversion (10 is the same as 10.0):

index.go

index.go

copy
1
var myFloat float32 = 10 // A valid statement

The other type of type casting is Explicit Type Casting, which occurs when the programmer explicitly converts data or expressions from one type to another. The syntax for explicit type casting is desiredType(expression), for example:

index.go

index.go

copy
12345678
package main import "fmt" func main() { var number1 float32 = 7.9 var number2 int = int(number1) fmt.Println(number2) // Outputs 7 }

In the above program, we convert a float32 value to an int using explicit type casting. As a result, the decimal part of the original number is discarded, and only the integral value 7 is stored in number2. It's important to note that in this case, some data is lost, specifically the decimal part of the number (0.9). However, we made that choice explicitly.

Similarly, we can convert a rune into a string. In the Runes chapter, we explored a program that outputted the value of a Rune, which was a number. However, we can display a Rune's character equivalent by converting it into a string:

index.go

index.go

copy
123456789
package main import "fmt" func main() { var char rune = 'a' fmt.Println(char) // Outputs 97 fmt.Println(string(char)) // Outputs 'a' }

However, we cannot convert a string into a rune:

index.go

index.go

copy
12
var myString string = "A string value" fmt.Println(rune(myString)) // Error: cannot convert myString (variable of type string) to type rune

It is important to note we can also not convert a string containing a single character into a rune:

index.go

index.go

copy
1234567
package main import "fmt" func main() { var text string = "a"; fmt.Printf(rune(text)) // Error here }
question mark

Which of the following is the correct syntax for type casting in Go?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 16

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Abschnitt 1. Kapitel 16
some-alt