13
votes

trying to have a type Char that is a string one character long. what I'm unable to do is create a "constructor". I know I'm missing something completely obvious.

declare the Char type

  type Char string

can use that type with a declaration

  var c1 Char("abc")
  var c2 Char = "abc"

these are wrong: c1 and c2 need to be "a", not "abc"

what I really want is a "constructor" to limit Char to one character

 func Char( s string ) Char {
   var ch string = s[0]
   return ch
 }

of course having the type Char and func Char is not the way to do it

 type.go:8: Char redeclared in this block

is there any way for to force type initialization through a constructor? or am I even asking the correct question?

let me state differently: if the user says var c Char = "abc" they will have an invalid value for type Char - is there any way to force the user into func NewChar(string) Char as Char's only valid constructor?

4
It's the Go programming language, not golang.peterSO
sorry about the golang - thought I was seeing that as a standard ref. the difference between goggling for "go" vs "golang" is huge, so have always tried to put the phrase "golang" as a courtesy, but open to suggestionscc young
If you want to force constructor, you should put your code in a separate package, expose public interface and a constructor that will return a private struct that implements the interface.Shay Tsadok

4 Answers

9
votes

This is the char package. Note the unexported Char struct field c.

package char

type Char struct {
    c rune
}

func New(c rune) *Char {
    return &Char{c}
}

func (c *Char) Char() rune {
    return c.c
}

func (c *Char) String() string {
    return string(c.c)
}

Here's an example of how to use the char package.

package main

import (
    "char"
    "fmt"
)

func main() {
    var c = char.New('z')
    var d = c.Char()
    hello := "Hello, world; or สวัสดีชาวโลก"
    h := []rune(hello)
    ก := char.New(h[len(h)-1])
    fmt.Println(c, "a-"+c.String(), '0' <= d && d <= '9', ก)
}

Output:

z a-z false ก
2
votes

At the first, see following example.

package main

func main() {
    s := "hello 世界"
    //for getting characters from string

    cells := []int(s)
    for _, c := range cells {
        println(c, string(c))
        // You'll get
        // 104 h
        // 101 e
        // 108 l
        // 108 l
        // 111 o
        // 32  
        // 19990 世
        // 30028 界
    }

    bytes := []byte(s)
    for _, b := range bytes {
        println(b, string(b))
        // 104
        // 101
        // 108
        // 108
        // 111
        // 32
        // 228
        // 184
        // 150
        // 231
        // 149
        // 140
    }
}

The meaning of []int(s) is "cast to unicode characters". The meaning of []byte(s) is "cast to bytes".

And, Go does not have constructor. In Go's style, package provide function NewXXX() for XXX object.

type Client struct {
    // ...
}

func NewClient() *Client {
    return &Client{/* ... */}
}

UPDATE:

If you mean Char as "1 element of string", you should define as following.

type Char int

Or

type Char byte

Your definition

type Char string

is re-define of "string". Then it can store string.

0
votes

A single unicode character in Go is represented by a uint32 You could do the following:

type Char uint32
var a Char = 'a'

No need for a constructor, since you can just use the character literal.

0
votes

Along with the other answers, it should be noted that Go has no such thing as special constructor syntax. There are some conventions though. Generally when you have a custom type and need a constructor, you write a NewT() function which takes any parameters you need, performs initialization and returns one instance of said type.

type Char string

func NewChar(s string) Char {
    if len(s) == 0 {
       return Char("")
    }
    return Char(s[:1])
}