Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 30, 2021 04:49 am GMT

Variabel on Golang

so now i will tell about variable in golang.

in golang, when the variable declaration must be followed by its data type. this concept is manifest typing . an example like this

package mainimport "fmt"func main(){   var firstName string = "Sakura "   var lastName string   lastName = "endo"   fmt.Printf("halo %s %s!
", firstName, lastName)}

the result is

result

like the code above, I write down the variable followed by the name and data type

How to declare variable using var

conditions in variable declaration with var like this

  1. Declaration without value
var <name-variable> <type-data>

2.Declaration with value

var <name-variable> <type-data> = <value>

Example

var Name stringvar Hobby string = "Football"

What is the use of "fmt.Printf()" ?

the function is the same as Println, but the output is defined at the beginning, in the "hello %s %s!
" section, %s will be replaced with a string where the string is filled with the value of the 2nd to 3rd parameter and so onof the parameter.

fmt.Printf("Hi %s %s", firstname,lastname)fmt.Printf("Hi "+  firstname + lastname + "!")

in addition to using "%s" to connect between strings, you can also use "+" .this is called string concatenation

Multi-variable declaration

This way, we can declare multiple variables with just 1 line.
Example:

var firts,second,third stringfirsts,second, third = "1","2","3"

easy and very simple,could be more concise this way

var firts,second,third string = "1","2","3"

or

firts,second,third := "1","2","3"

End

Thanks for reading, don't forget to like and share and that's how to declare variables in golang!.

Golang is so much fun


Original Link: https://dev.to/xvbnm48/variabel-on-golang-j1d

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To