1. Introduction
In Go, values are the fundamental units of data manipulation. From strings and integers to floating-point numbers and booleans, understanding how to work with these basic types is essential. This post will provide examples of using different types of values in Go.
Definition
Values in Go are instances of a type. Types define the kind of data a value can hold and what operations can be performed on that data. Common types include int, string, bool, and float64.
2. Program Steps
1. Declare values of different types.
2. Perform operations on those values.
3. Print the results of these operations.
3. Code Program
package main
import "fmt"
func main() {
// Strings, which can be added together with +
fmt.Println("go" + "lang")
// Integers and floats
fmt.Println("1+1 =", 1+1)
fmt.Println("7.0/3.0 =", 7.0/3.0)
// Booleans, with boolean operators as expected
fmt.Println(true && false)
fmt.Println(true || false)
fmt.Println(!true)
}
Output:
golang 1+1 = 2 7.0/3.0 = 2.3333333333333335 false true false
Explanation:
1. package main - Defines the main package for a Go program.
2. import "fmt" - Imports the fmt package which contains functions for formatting text, including printing to the console.
3. The fmt.Println function is used to print values. + is used to concatenate strings, + and / are used to add and divide integers and floats respectively, and && (and), || (or), and ! (not) are used as boolean operators.
4. Strings can be concatenated directly with the + operator.
5. Integers and floats are shown with addition and division examples, showcasing how Go handles arithmetic operations.
6. Booleans and boolean operations demonstrate logical operations in Go.
7. The output shows the results of each print statement, displaying the manipulation of various value types in Go.
Comments
Post a Comment