Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Create or initialize a new string in Go (Golang)

Posted on February 7, 2023February 7, 2023 by admin

Table of Contents

  • Overview
  • Program

Overview

Below is a simple way to initialize or create a string in Go. In the below program, we have simply declared as well as defined a string named sample

Notice the syntax

sample := "test"

Here is the full program

Program

package main
import "fmt"
func main() {
sample := "test"
fmt.Println(sample)
}

Output

test

If we want to only declare a string variable then below is the way. A declared string is initialized to an empty string

package main
import "fmt"
func main() {
var sample string
fmt.Println(sample)
}

Output

Note: Check out our Golang Advanced Tutorial. The tutorials in this series are elaborative and we have tried to cover all concepts with examples. This tutorial is for those who are looking to gain expertise and a solid understanding of golang – Golang Advance Tutorial

Also if you are interested in understanding how all design patterns can be implemented in Golang. If yes, then this post is for you –

All Design Patterns Golang

Note: Check out our system design tutorial series System Design Questions

Popular Articles

Golang Comprehensive Tutorial Series

All Design Patterns in Go (Golang)

Slice in golang

Variables in Go (Golang) – Complete Guide

OOP: Inheritance in GOLANG complete guide

Using Context Package in GO (Golang) – Complete Guide

All data types in Golang with examples

Understanding time and date in Go (Golang) – Complete Guide

©2023 Welcome To Golang By Example | Design: Web XP