user/group.go
func (g *Group) Add() (gid int, err error) {
	loadConfig()

	group, err := LookupGroup(g.Name)
	if err != nil {
		if _, ok := err.(NoFoundError); !ok {
			return 0, err
		}
	}
	if group != nil {
		return 0, ErrGroupExist
	}

	if g.Name == "" {
		return 0, RequiredError("Name")
	}

	var db *dbfile
	if g.GID < 0 {
		db, gid, err = nextGUID(g.addSystemGroup)
		if err != nil {
			db.close()
			return 0, err
		}
		g.GID = gid
	} else {
		db, err = openDBFile(_GROUP_FILE, os.O_WRONLY|os.O_APPEND)
		if err != nil {
			return
		}

		// Check if Id is unique.
		_, err = LookupGID(g.GID)
		if err == nil {
			return 0, IdUsedError(g.GID)
		} else if _, ok := err.(NoFoundError); !ok {
			return 0, err
		}
	}

	g.password = "x"

	_, err = db.file.WriteString(g.String())
	err2 := db.close()
	if err2 != nil && err == nil {
		err = err2
	}
	return
}
func (g *Group) Add() (gid int, err error) {
	loadConfig()

	group, err := LookupGroup(g.Name)
	if err != nil {
		if _, ok := err.(NoFoundError); !ok {
			return 0, err
		}
	}
	if group != nil {
		return 0, ErrGroupExist
	}

	if g.Name == "" {
		return 0, RequiredError("Name")
	}

	var db *dbfile
	if g.GID < 0 {
		db, gid, err = nextGUID(g.addSystemGroup)
		if err != nil {
			db.close()
			return 0, err
		}
		g.GID = gid
	} else {
		db, err = openDBFile(_GROUP_FILE, os.O_WRONLY|os.O_APPEND)
		if err != nil {
			return
		}

		// Check if Id is unique.
		_, err = LookupGID(g.GID)
		if err == nil {
			return 0, IdUsedError(g.GID)
		} else if _, ok := err.(NoFoundError); !ok {
			return 0, err
		}
	}

	g.password = "x"

	_, err = db.file.WriteString(g.String())
	err2 := db.close()
	if err2 != nil && err == nil {
		err = err2
	}
	return
}
// Add adds a new group.
// Whether GID is < 0, it will choose the first id available in the range set
// in the system configuration.